Reputation: 196459
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
Upvotes: 6
Views: 3338
Reputation: 1062715
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.
Upvotes: 14