Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reusing the backgroundworker more than once

i am using the background worker to do an expensive operation:

backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.RunWorkerAsync(inputs);

At the end i have this:

 void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   Messagebox.Show("Done with expensive operation 1"};
}

I now have another expensive operation. Can i reuse this same background worker. I now want new callbacks as i dont want switch statements on the ProgressChanged and DoWork Callbacks to determine if i am doing operation 1 or 2.

Is it just simpler to use 2 seperate background worker classes

like image 465
leora Avatar asked Mar 25 '09 10:03

leora


1 Answers

Yes, you can re-use a BackgroundWorker - but you can only use it once at any time - not concurrently. If the operations are different, however, I'd use a separate worker. Otherwise you'll have to unhook the events, hook the correct events, etc. Messy.

like image 183
Marc Gravell Avatar answered Nov 11 '22 11:11

Marc Gravell