C# 此 BackgroundWorker 当前正忙,无法同时运行多个任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/588150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
This BackgroundWorker is currently busy and cannot run multiple tasks concurrently
提问by
I get this error if I click a button that starts the backgroundworker twice.
如果我单击启动后台工作程序两次的按钮,我会收到此错误。
This BackgroundWorker is currently busy and cannot run multiple tasks concurrently
How can I avoid this?
我怎样才能避免这种情况?
采纳答案by Orion Edwards
Simple: Don't start the BackgroundWorker twice.
简单:不要启动 BackgroundWorker 两次。
You can check if it is already running by using the IsBusy
property, so just change this code:
您可以使用该IsBusy
属性检查它是否已经在运行,因此只需更改以下代码:
worker.RunWorkerAsync();
to this:
对此:
if( !worker.IsBusy )
worker.RunWorkerAsync();
else
MessageBox.Show("Can't run the worker twice!");
Update:
更新:
If you do actually need to launch multiple background tasks at the same time, you can simply create multiple BackgroundWorker objects
如果您确实需要同时启动多个后台任务,您可以简单地创建多个 BackgroundWorker 对象
回答by Justin R.
Create a new BackgroundWorker object for each operation that you want to perform. I.e., rather than:
为您要执行的每个操作创建一个新的 BackgroundWorker 对象。即,而不是:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
for (int i; i < max; i++) {
worker.RunWorkerAsync(i);
}
Try this:
尝试这个:
for (int i; i < max; i++) {
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync(i);
}
回答by Thies
I would look into queue'ing the tasks that need to be done. You get the following advantages;
我会研究排队需要完成的任务。您将获得以下优势;
- You do not have to handle the problems of background tasks not being able to start due to something else already running
- You do not have to worry about using up too many threads, by creating a new background worked for each task.
- Lastly the queue couldallow you to ensure that the background tasks run in the same order as they where created/requested.
- 您不必处理由于其他东西已经在运行而导致后台任务无法启动的问题
- 通过为每个任务创建一个新的背景,您不必担心使用太多线程。
- 最后,队列可以让您确保后台任务以与创建/请求时相同的顺序运行。
Here is an example implementation: http://thevalerios.net/matt/2008/05/a-queued-backgroundworker. I am not sure if the implementation in threadsafe, and I will update my answer once I figure out of my current locking problem in a implementation I am working with.
这是一个示例实现:http: //thevalerios.net/matt/2008/05/a-queued-backgroundworker。我不确定线程安全中的实现,一旦我在我正在使用的实现中找出当前的锁定问题,我将更新我的答案。
回答by David Refaeli
Although not the case originally asked by the OP, this can also happen due to a race condition (happened to me now, and looked for an answer) if you're using a Background worker in some sort of a producer-consumer pattern.
虽然不是 OP 最初要求的情况,但如果您在某种生产者-消费者模式中使用后台工作者,这也可能由于竞争条件(现在发生在我身上,并寻找答案)而发生。
Example:
例子:
if (BckgrndWrkr == null)
{
BckgrndWrkr = new BackgroundWorker();
BckgrndWrkr.DoWork += DoWorkMethod;
BckgrndWrkr.RunWorkerAsync();
}
else if (!BckgrndWrkr.IsBusy)
{
BckgrndWrkr.RunWorkerAsync();
}
In this case there's a race condition: first instance instantiates a new background worker, 2nd instance reaches the else if
and starts the background worker, before 1st instance reaches the RunWorkerAsync of the if
block, and when it does it throws the error.
在这种情况下有一个竞争条件:第一个实例实例化一个新的后台工作器,第二个实例到达else if
并启动后台工作器,在第一个实例到达if
块的 RunWorkerAsync 之前,当它发生时它抛出错误。
This can be avoided by adding a lock to the entire if + if else section.
这可以通过向整个 if + if else 部分添加锁来避免。