The Right Way to Dispose of BackGroundWorker

Would this be the right way to get rid of BackGroundWorker? I'm not sure if events need to be deleted before calling .Dispose (). .Dispose () is also called inside the RunWorkerCompleted ok delegate?

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(); } 
+43
multithreading c # backgroundworker
Mar 30 '10 at 1:36
source share
5 answers

BackgroundWorker comes from Component. The component implements the IDisposable interface. This in turn causes BackgroundWorker to inherit the Dispose () method.

Retrieving from a Component is a convenience for Windows Forms programmers; they can drop BGW from the toolbar into the form. Components in general are more likely to have something to dispose of. Windows Forms Designer will take care of this automatically, look in the Designer.cs file for the form for the "components" field. Its automatically generated Dispose () method calls the Dispose () method for all components.

However, BackgroundWorker does not actually have a member that requires recycling. It does not override Dispose (). Its basic implementation, Component.Dispose (), only guarantees that the component will be removed from the "components" collection. And raise the Disposed event. But otherwise he does not manage anything.

In short: if you dumped BGW on the form, then everything takes care automatically, you do not need to help. If you did not drop it on the form, this is not an element in the component collection, and nothing needs to be done.

You do not need to call Dispose ().

+64
Mar 30 '10 at 1:53
source share

Late for the game, but I just ran into a scenario related to your question, which I thought I would share. If you create your employee at the class level and reuse it in sequential operations without closing the application, if you do not delete events after completion, they will grow and run several times with each subsequent execution.

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

Without the above, my DoWork fires once a first time, twice a second time, etc. It probably doesn't matter much for most, but it took me a bit to figure it out, so hopefully it helps someone else.

+12
Jul 07 2018-11-11T00:
source share

worker.Dispose() not required since Dispose() automatically called. But before deleting the object, you need to delete all event handlers.

This article tells us about it.

 worker.RunWorkerCompleted -= new RunWorkerCompletedEventHandle(worker_RunWorkerCompleted); worker.DoWork -= new DoWorkEventHandler(worker_DoWork); 
+1
Feb 22 '12 at 10:05
source share

Yes, that seems right. Of course, disposable objects are better handled using using blocks, but you don't have this option here.

I usually create my forms lifecycle assistants, reuse them, and let the designer code handle the deletion on the form. Think less.

0
Mar 30 '10 at 1:39
source share

If it is in the "WinForms" form, let the container take care of this (see the generated Dispose code in the Form.Designer.xyz file)

In practice, I found that you might need to create an instance of the container and add a working (or other related) container to it if someone knows a more official way to make it shout!

PK :-)

 public partial class Form1 : Form { public Form1() { InitializeComponent(); // watch the disposed event.... backgroundWorker1.Disposed += new EventHandler(backgroundWorker1_Disposed); // try with and without the following lines components = new Container(); components.Add(backgroundWorker1); } void backgroundWorker1_Disposed(object sender, EventArgs e) { Debug.WriteLine("backgroundWorker1_Disposed"); } //... from the Designer.xyz file ... /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } } 
0
Mar 30
source share



All Articles