Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to Dispose of a BackGroundWorker

Would this be a proper way to dispose of a BackGroundWorker? I'm not sure if it is necesary to remove the events before calling .Dispose(). Also is calling .Dispose() inside the RunWorkerCompleted delegate ok to do?

public void RunProcessAsync(DateTime dumpDate) {     BackgroundWorker worker = new BackgroundWorker();     worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);     worker.DoWork += new DoWorkEventHandler(worker_DoWork);     worker.RunWorkerAsync(dumpDate); }  void worker_DoWork(object sender, DoWorkEventArgs e) {     // Do Work here }  void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {     BackgroundWorker worker = sender as BackgroundWorker;     worker.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);     worker.DoWork -= new DoWorkEventHandler(worker_DoWork);     worker.Dispose(); } 
like image 386
galford13x Avatar asked Mar 30 '10 01:03

galford13x


People also ask

What is use of BackgroundWorker in C#?

BackgroundWorker is the class in System. ComponentModel which is used when you need to do some task on the back-end or in different thread while keeping the UI available to users (not freezing the user) and at the same time, reporting the progress of the same.

Is BackgroundWorker threaded?

BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.

What is BackgroundWorker in Winforms?

The BackgroundWorker class exposes the DoWork event, to which your worker thread is attached through a DoWork event handler. The DoWork event handler takes a DoWorkEventArgs parameter, which has an Argument property.


2 Answers

BackgroundWorker derives from Component. Component implements the IDisposable interface. That in turn makes BackgroundWorker inherit the Dispose() method.

Deriving from Component is a convenience for Windows Forms programmers, they can drop a BGW from the toolbox onto a form. Components in general are somewhat likely to have something to dispose. The Windows Forms designer takes care of this automatically, look in the Designer.cs file for a Form for the "components" field. Its auto-generated Dispose() method calls the Dispose() method for all components.

However, BackgroundWorker doesn't actually have any member that requires disposing. It doesn't override Dispose(). Its base implementation, Component.Dispose(), only makes sure that the component is removed from the "components" collection. And raise the Disposed event. But doesn't otherwise dispose anything.

Long story short: if you dropped a BGW on a form then everything is taken care of automatically, you don't have to help. If you didn't drop it on a form then it isn't an element in a components collection and nothing needs to be done.

You don't have to call Dispose().

like image 183
Hans Passant Avatar answered Sep 19 '22 23:09

Hans Passant


Late to the game, but I just ran across a scenario related to your question that I thought I would share. If you create your worker at the class level and reuse it on successive operations without closing the app, if you don't remove the events after completion they will increment and run multiple times on each successive execution.

worker.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.DoWork -= new DoWorkEventHandler(worker_DoWork); 

Without the above my DoWork fires once the first time, twice the second time, etc. This is probably a no-brainer for most, but it took me a bit to figure it out, so hopefully this will help someone else out.

like image 34
Paul Avatar answered Sep 19 '22 23:09

Paul