Search code examples
c#backgroundworker

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?


Solution

  • 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