开发者

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

开发者 https://www.devze.com 2023-01-08 05:53 出处:网络
My WinForm calls a class which performs some copying actions. I\'d like to show the progress of this on a form.

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 Backgrou开发者_开发百科ndworker, but I don't know how to report progress from the class to the form (/backgroundworker)


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.


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.


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


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

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消