How do I update backgroundWorker.ReportProgress () through a class in another project?

4

I read a text file that contains data from another database, load that data line by line into a class, edit the required fields, and then save it to the new database.

This insert processing follows the following pattern:

In View , I select the text file and the step by parameter for the Controller , which in turn calls the Model , which does all the processing and calls the AcessoADados to save to the database. data.

In%% of Form , I have a View , where event BackGroundWorker calls Do_Work to start the process.

I also have a Controller , which updates its values through progressBar to inform how many the load is.

My question is:

How do I tell backgroundWorker.ReportProgress() that progress is increasing, with each record inserted, there in class BackGroundWorker ?

    
asked by anonymous 05.02.2014 / 10:59

2 answers

1

The strategy I would use would be to pass a delegate that serves to notify progress, which will be called by the other thread whenever progress is made, and then within that delegate, would update the form.

So, your save method is not dependent on interface elements, but rather on a delegate that can notify progress for whatever UI system.

Note

In Windows Forms, you will need to check the InvokeRequired property of your progress% delegate within the progress report delegate code to see if you need to use the Form method to be able to make changes to the user interface.

    
05.02.2014 / 17:05
1

It's simple, you have to pass the reportProgress method as a parameter

class FormView
{
    private void Something_DoWork(object sender, DoWorkEventArgs e) 
    {
        new ModelClass().Insert((sender as BackgroundWorker).ReportProgress);
    }   
}

class ModelClass
{
    public void Insert(Action<int> reportProgress)
    {
    }
}
    
05.02.2014 / 14:03