BackgroundWorkers never stop being busy BackgroundWorkers never stop being busy multithreading multithreading

BackgroundWorkers never stop being busy


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 BackgroundWorkers 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.