多次重用后台工作者
我正在使用后台工作人员执行一项昂贵的操作:
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.RunWorkerAsync(inputs);
最后我有这样的:
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Messagebox.Show("Done with expensive operation 1"};
}
我现在有另一个昂贵的操作。 我可以重用这个相同的后台工作者吗? 我现在想要新的回调,因为我不想在 ProgressChanged 和 DoWork 回调上使用 switch 语句来确定我是否正在执行操作 1 或 2。
使用 2 个单独的后台工作类是否更简单
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以重复使用
BackgroundWorker
- 但您只能随时使用它一次 - 不能同时使用。 但是,如果操作不同,我会使用单独的工作人员。 否则你将不得不取消事件的挂钩,挂钩正确的事件,等等。混乱。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.