views:

120

answers:

4

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)

+5  A: 

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.

ChrisA
So I have to add a public background property in my class and the form has to register to the progresschanged event?
Martijn
+3  A: 

Hi,

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.

andyp
How would you expose the ProgressChanged event to the form?
Martijn
I just updated my answer to show that.
andyp
+2  A: 

It is done via the ReportProgress instance method of your current BackgroundWorker object. Your form must subscribe to the ProgressChanged event.

MSDN has a small example here: http://msdn.microsoft.com/en-us/library/waw3xexc%28v=VS.100%29.aspx

Andreas
How does my form subscribe itself to the ProgressChanged event?
Martijn
`backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler( backgroundWorker1_ProgressChanged);` where `backgroundWorker1_ProgressChanged` is the name of your event handling method, prototyped by the delegate `ProgressChangedEventHandler`.
Andreas
I don't get it, sorry. I still don't see how my Form can register to the `ProgressChanged` event. In your case there's a method (`backgroundWorker1_ProgressChanged`) which handles the event. But my form needs to handle the event, not my class..
Martijn
What do you mean by `my class`? A Form is usually represented by a derived class. You need not derive your own class from `BackgroundWorker`. Maybe show us some code?
Andreas
+1  A: 

call in your class

 backgroundWorker.ReportProgress(i++);

on a form handler an event

//declare progressBar1 in the form and set range 
// progressBar1.Minimum = 0;
//progressBar1.Maximum = 100;
 private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
       //show progress bar

    }
Arseny
In this case, how do I expose the ProgressChanged event in my class?
Martijn
I'd just give your class an event that's raised whenever the ProgressChanged event of the BackgroundWorker is raised. Your event would have the same signature as the BackgroundWorker's.
andyp
You get the value passed to `ReportProgress` from the `ProgressPercentage` instance field of the passed `ProgressChangedEventArgs`, i.e. `progressBar1.Value = e.ProgressPercentage;`.
Andreas
@Martijn you need to set BackgroundWorker reference to your class to be able to call Reportprogress method
Arseny