Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to report progress from within a class to a BackgroundWorker?

My WinForm calls a class which performs some copying actions. I'd like to show the progress of this on a form.

I'd like to use the Backgroundworker, but I don't know how to report progress from the class to the form (/backgroundworker)

like image 816
Martijn Avatar asked Jul 20 '10 12:07

Martijn


2 Answers

use the OnProgressChanged() method of the BackgroundWorker to report progress and subscribe to the ProgessChangedEvent of the BackgroundWorker to update the progress in your GUI.

Your copy class knows the BackgroundWorker and subscribes to ProgressChanged. It also exposes an own ProgressChanged event that's raised by the event handler for the background worker's ProgressChanged event. Finally your Form subscribes to the ProgressChanged event of the copy class and displays the progress.

Code:

public class CopySomethingAsync
{
    private BackgroundWorker _BackgroundWorker;
    public event ProgressChangedEventHandler ProgressChanged;

    public CopySomethingAsync()
    {
        // [...] create background worker, subscribe DoWork and RunWorkerCompleted
        _BackgroundWorker.ProgressChanged += HandleProgressChanged;
    }

    private void HandleProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        if (ProgressChanged != null)
            ProgressChanged.Invoke(this, e);
    }
}

In your form just subscribe to the ProgressChanged event of CopySomethingAsync and display the progress.

like image 111
andyp Avatar answered Sep 27 '22 21:09

andyp


Everything you need to know about BackgroundWorker is on msdn.

As it says in the article:

To receive notifications of progress updates, handle the ProgressChanged event.


Update:

Having read Martijn's supplementary questions, and given that he has a class which hitherto has been doing his work, presumably on the foreground thread, I'd add the following:

  • The worker class has responsibility for the work, so it also has responsibility for reporting on its progress. The fact that it spawns a background thread to do the work is not the concern of the Form.

  • So, I'd be inclined to have the class set up the BGW, and handle its ProgressChanged events, and then raise its own events (on the foreground thread) to which the form itself could then subscribe. I do a ton of WinForms coding using this technique and it works fine.

The alternative would be to expose the BGW as a public property of the worker class, and have the form handle its events directly. But I don't like this so much, since it makes the form dependent on the implementation of the worker class. This is generally A Bad Thing.

like image 27
ChrisA Avatar answered Sep 27 '22 20:09

ChrisA