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();
}
}
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 ?
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..
--[edit request]---
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);
}
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 ?
---[EDIT ANSWER]---- it worked by JUST replacing the line
System.Threading.Thread.Sleep(0001);
with
Application.DoEvents();
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!
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.
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.
I suggest you change your code to handle the RunWorkerCompleted
event to get notifications when your BackgroundWorker
s are finished with their work. There is an example of how to use the BackgroundWorker
in the official documentation.
I had this same problem whilst using background workers and came the conclusion that if you use sleep() within a loop, then it will get stuck. You can either use the RunWorkerCompleted event and set a boolean flag to indicate when each worker is finished.
Or if you want to abort the thread regardless you could look at using threads instead of background workers. However then you lose the ease of use in regards to the events which the background worker provides.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With