此BackgroundWorker当前正忙,无法同时运行多个任务

发布于 2024-07-14 00:47:36 字数 166 浏览 6 评论 0原文

如果我单击启动后台工作程序两次的按钮,我会收到此错误。

This BackgroundWorker is currently busy and cannot run multiple tasks concurrently

我怎样才能避免这种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瀞厅☆埖开 2024-07-21 00:47:36

简单:不要启动BackgroundWorker 两次。

您可以使用 IsBusy 属性检查它是否已经在运行,因此只需将此代码更改

worker.RunWorkerAsync();

为:

if( !worker.IsBusy )
    worker.RunWorkerAsync();
else
    MessageBox.Show("Can't run the worker twice!");

更新:

如果您确实需要同时启动多个后台任务,您可以简单地创建多个BackgroundWorker对象

Simple: Don't start the BackgroundWorker twice.

You can check if it is already running by using the IsBusy property, so just change this code:

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

王权女流氓 2024-07-21 00:47:36

为您要执行的每个操作创建一个新的BackgroundWorker 对象。 即,而不是:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
for (int i; i < max; i++) {
   worker.RunWorkerAsync(i);
}

试试这个:

for (int i; i < max; i++) {
   BackgroundWorker worker = new BackgroundWorker();
   worker.DoWork += new DoWorkEventHandler(worker_DoWork);
   worker.RunWorkerAsync(i);
}

Create a new BackgroundWorker object for each operation that you want to perform. I.e., rather than:

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);
}
尽揽少女心 2024-07-21 00:47:36

我会考虑对需要完成的任务进行排队。 您将获得以下优势:

  • 您不必处理后台任务由于其他已在运行的任务而无法启动的问题
  • 通过为每个任务创建一个新的后台,您不必担心使用太多线程。
  • 最后,队列可以让您确保后台任务按照创建/请求的顺序运行。

这是一个示例实现: 链接。 我不确定该实现是否是线程安全的,一旦我弄清楚我正在使用的实现中当前的锁定问题,我将更新我的答案。

I would look into queueing 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 could allow you to ensure that the background tasks run in the same order as they where created/requested.

Here is an example implementation: link. 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.

爱你是孤单的心事 2024-07-21 00:47:36

虽然不是OP最初询问的情况,但如果您以某种生产者-消费者模式使用后台工作人员,这也可能由于竞争条件而发生(现在发生在我身上,并寻找答案)。

示例:

if (BckgrndWrkr == null)
{
    BckgrndWrkr = new BackgroundWorker(); 
    BckgrndWrkr.DoWork += DoWorkMethod;
    BckgrndWrkr.RunWorkerAsync();     
}
else if (!BckgrndWrkr.IsBusy)
{
    BckgrndWrkr.RunWorkerAsync();
}

在这种情况下,存在竞争条件:第一个实例实例化一个新的后台工作线程,第二个实例到达 else if 并启动后台工作线程,然后第一个实例到达 if< 的 RunWorkerAsync /code> 块,当它执行时它会抛出错误。

可以通过向整个 if + if else 部分添加锁来避免这种情况。

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.

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.

This can be avoided by adding a lock to the entire if + if else section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文