reusing the backgroundworker more than once

假装没事ソ 提交于 2019-11-30 05:07:46

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/681076/reusing-the-backgroundworker-more-than-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!