How to update backgroundWorker.Reportprogress() through a class in another project?

Asked

Viewed 599 times

4

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

This insertion processing follows the following standard:

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

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

I also have a progressBar, that updates its values through the backgroundWorker.ReportProgress() to indicate how many loads are being loaded.

My question is:

How to inform the BackGroundWorker that progress is increasing, with each record inserted, there of the class Model?

  • Guys, so far no response has worked. When the Backgroundworker and the information processing are in the same class, I can do. The problem is when the processing and the Backgroundworker are in Classes of Projects different.

2 answers

1


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

Therefore, your rescue method is not dependent on interface elements, but rather on a delegate who can notify progress to whatever UI system.

Note

When it comes to Windows Forms, you will need to check within the delegate code for progress notification, the property InvokeRequired of your Form, in order to know whether it is necessary to use the Invoke in order to make changes to the user interface.

1

It’s simple, you have to pass the reportProgress method as 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)
    {
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.