This BackgroundWorker is currently busy and cannot run multiple tasks concurrently

vb net this backgroundworker is currently busy and cannot run multiple tasks concurrently
c# stop backgroundworker
backgroundworker cancellationpending

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?

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

VS 2013 BackgroundWorker is currently busy and cannot run , This BackgroundWorker is currently busy and cannot run multiple tasks concurrently · c# wpf backgroundworker. I'm trying to use a Background  This BackgroundWorker is currently busy and cannot run multiple tasks concurrently So I did some Googling and it seems that I can enable the .WorkerSupportsCancellation property of the background worker and just .CancelAsync() .

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);
}

C# This BackgroundWorker is currently busy and cannot run , Re: BackgroundWorker is currently busy and cannot run multiple tasks concurrently. As Shaggy suggests, you are rather misusing the BackgroundWorker. As the name suggests, it is for doing background work. This BackgroundWorker is currently busy and cannot run multiple tasks concurrently So I did some Googling and it seems that I can enable the .WorkerSupportsCancellation property of the background worker and just .CancelAsync() .

https://social.msdn.microsoft.com/Forums/en-US/19e, C# This BackgroundWorker is currently busy and cannot run multiple tasks concurrently. I get this error if I click a button that starts the  BackgroundWorker is currently busy and cannot run multiple tasks concurrently. I have a background worker to run a loop that queries the database until a value is returned. It needs to launch on load and on refresh.

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.

[.net] C# how to stop backgroundworker??, the following error: "This BackgroundWorker is currently busy and cannot run multiple tasks concurrently." Below are some code fragments of  Thread used for Serial port data processing throwing “This BackgroundWorker is currently busy and cannot run multiple tasks concurrently” 0 VB.Net Asynchronous background worker inside a loop

Unknown errorThis BackgroundWorker is currently busy, The Exception is like "This BackgroundWorker is currently busy and cannot run multiple tasks concurrently". Description: I got this  System.InvalidOperationException: This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.

OnCurrentChanged, ErrorCode.001 -- This BackgroundWorker is currently busy and cannot run multiple tasks concurrently. [System.InvalidOperationException]  Thread used for Serial port data processing throwing “This BackgroundWorker is currently busy and cannot run multiple tasks concurrently”

caused InvalidOperationException (This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.) in assembly  This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.

Comments
  • To some degree I agree that the answer isn't fully useful. Given the case you do need to run two background processes, it isn't great to get an error instead that says "Can't run the worker twice". I would assume the answer about running new backgroundWorkers for each task - or in some way queing the jobs is a better solution, that won't cause you to have to check if some other part of the system has already started the background job and act uppon that.
  • when exactly does IsBusy get cleared? i would have expected that it gets cleared just before the RunWorkerCompleted event is fired, but this doesn't seem to be the case (at least based on my experiments).
  • That's a great answer. How do we kill all of the backgroundWorkers if required?
  • @VladSpreys I can't really discuss thread cancellation in just a comment, but there are some really good posts on this site on the subject.
  • @fatcat1111 thank you. I understand the idea of canceling the thread if we keep a reference to it. But with this code snippet we don't.
  • @VladSpreys if you want a list of references to the workers, then outside of the for loop declare a List<BackgroundWorker>, and in the loop, after instantiating the worker, add it to the list. Then if you need to cancel them, walk the list and cancel each by whatever means is best for your scenario.
  • The link to the example implementation is broken
  • Please don't ever name your variables like this.