重用的BackgroundWorker不止一次 [英] reusing the backgroundworker more than once

查看:122
本文介绍了重用的BackgroundWorker不止一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是后台工作做一个昂贵的操作:

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"};
}

我现在有另一个昂贵的操作。我可以重复使用同样的后台工作。我现在想新的回调,因为我不想开关在ProgressChanged和DoWork的回调语句,以确定是否我在做什么操作1或2。

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.

难道只是简单的使用2个独立的后台工作类

Is it just simpler to use 2 seperate background worker classes

推荐答案

是的,你可以重新使用的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.

这篇关于重用的BackgroundWorker不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆