Differences of Delegates vs BackgroundWorker?

2019-06-09 04:07发布

问题:

Can anyone explain the difference between Delegates and BackgroundWorker?In which case Backgroundworker is more efficient than Delegate?Since we are having asynchronous delegate what is the need to use BackGroungWorker.

回答1:

BackgroundWorker:

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread.

Delegate:

A delegate is a type that defines a method signature. ... Delegates are used to pass methods as arguments to other methods.


The question of which one to use has nothing to do with efficiency.

BackgroundWorker is a wrapper that simplifies working with threads, you can use async delegates just as well, but managing them correctly is much more difficult. Or, from MSDN:

When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.



回答2:

I have a brief comparison of BackgroundWorker, asynchronous delegates, and other approaches on my blog (from the perspective of doing background operations).

BackgroundWorker has these advantages:

  • Progress reporting is easy. Any BackgroundWorker whose WorkerSupportsProgress property is true may report progress. The DoWork delegate may invoke ReportProgress, which causes the ProgressChanged event to fire.
  • There's a built-in system for cooperative cancellation. The cancelling thread first calls BackgroundWorker.CancelAsync. This causes the BackgroundWorker.CancellationPending property to become true. The DoWork delegate should monitor that property (checking it on a regular basis), and set DoWorkEventArgs.Cancel to true and return if the operation is cancelled. The RunWorkerCompleted delegate detects a cancelled result by checking RunWorkerCompletedEventArgs.Cancelled.
  • Synchronization is automatic, for both completion indications and progress reports. The ProgressChanged and RunWorkerCompleted events are synchronized to the SynchronizationContext that was in place when RunWorkerAsync was called.

Asynchronous delegates have this advantage:

  • Returning a value is straightforward; it's just returned.

In conclusion, I recommend using Task<TResult> instead of either BackgroundWorker or asynchronous delegates.



回答3:

The background worker is primarily for UI work where you need to easily run a task on a background thread and provide progress updates to the screen.

One advantage is it marshalls its callbacks to the UI thread for you so you don't need to check if InvokeRequired etc.

Delegates are a more general mechanism for passing functions as arguments and by executing them asynchronously they give you an easy way of running those methods on another thread.



回答4:

Background worker is an abstraction to help you execute an operation on a separate thread.

Delegates don't really start separate threads - they are a type for referring to methods.

But what you are probably interested in is when you should use asynchronous methods instead of using background worker. I have not much experience in this, but Anders Hejlsberg talked about it some in his PDC session on the Future of C#.

The message I got was that in some circumstances asynchronous methods would be preferable because of lower complexity. The UI thread would still be blocked, but not for so long that it mattered.