BackgroundWorkers从来没有停止忙 [英] BackgroundWorkers never stop being busy

查看:535
本文介绍了BackgroundWorkers从来没有停止忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for (do it a bunch of times)
{         
    while (backgroundWorker1.IsBusy && backgroundWorker2.IsBusy &&
           backgroundWorker3.IsBusy && backgroundWorker4.IsBusy &&
           backgroundWorker5.IsBusy)
    {
        System.Threading.Thread.Sleep(0001);
    }

    if (!backgroundWorker1.IsBusy)
    {
        backgroundWorker1.RunWorkerAsync();
    }
    else if (!backgroundWorker2.IsBusy)
    {
        backgroundWorker2.RunWorkerAsync();
    }
    else if (!backgroundWorker3.IsBusy)
    {
        backgroundWorker3.RunWorkerAsync();
    }
    else if (!backgroundWorker4.IsBusy)
    {
        backgroundWorker4.RunWorkerAsync();
    }
    else if (!backgroundWorker5.IsBusy)
    {
        backgroundWorker5.RunWorkerAsync();
    }
}

运行5次(每次BG-工人一次),并卡在一段时间。不要将backgroundworkers永远停止正在忙吗?我怎么查询?

it runs five times (every BG-worker once) and gets stuck in the while. Don't the backgroundworkers ever stop being busy ? how do I check availability ?

请注意:有5个工作线程,这样保证其中没有一个是以往任何时候都停止了,总是分配工作给他们。但他们拒绝告诉我的时候都可以,我认为将有一个简单的解决方案。

note: there are 5 worker threads, this assures none of them is ever stopped, always assigning work to them. But they refuse to tell me when they are available, I thought that would have a simple solution..

- ---

其实这只是一个虚拟的参数,我删除它,忘了把它弄出来,我只用它来调用DoWork的,谁做的肮脏的工作:

Actually it was only a dummy parameter, i removed it and forgot to get it out, I only use it to call the dowork, who does the dirty job:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    timeconsumingfunction(publicstring);
}

而timeconsumingfunction DOES结束。步入这在调试和运行的每行线,它会直到结束,到达最后的}。这意味着它结束了吧?

And the timeconsumingfunction DOES end. stepping into it in the debugger and running line per line, it goes until the end and arrives in the final '}'. That means it ends, right ?

--- ---- 它的工作,只需更换行

------- it worked by JUST replacing the line

System.Threading.Thread.Sleep(0001);

Application.DoEvents();

我想它会运行的后台,但没有得到答案,而不是更新IsBusy标签。

I guess it would run the background, but not receive the answer and not update the IsBusy tags.

谢谢大家,伟大的答案,帮助了很多!

Thanks everybody, great answers, helped a lot!

推荐答案

您环路导致僵局,BGWs无法完成。问题是RunWorkerCompleted事件,它提高了UI线程。对BGW魔法位需要UI线程被闲置,必须抽它的消息循环。问题是,在UI线程是没有闲着,它是不抽的消息,它被卡在for循环。因此,该事件处理程序不能运行,IsBusy保持为真。

Your loop is causing deadlock, the BGWs cannot complete. The problem is the RunWorkerCompleted event, it is raised on the UI thread. That bit of BGW magic requires the UI thread to be idle, it must be pumping its message loop. Problem is, the UI thread is not idle and it isn't pumping messages, it is stuck in the for loop. Thus, the event handler cannot run and IsBusy stays true.

您将需要不同的方法来做这个。杠杆RunWorkerCompleted事件运行code,你会通常在此之后运行循环。抵制一切诱惑,调用Application.DoEvents()内循环。

You'll need to do this differently. Leverage the RunWorkerCompleted event to run the code that you'd normally run after this for loop. Resist all temptation to call Application.DoEvents() inside the loop.

这篇关于BackgroundWorkers从来没有停止忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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