views:

5810

answers:

4

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?

Thanks

+18  A: 

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!");
Orion Edwards
100% correct, 0% useful, 60% funny = +1
Bill K
Why 0% udeful and why 60% funny? What am I missing? Thanks
Ummm...how does 100% correct == 0% useful?
Ed Swangren
@Ed, I can tell you with 100% accuracy where you are at this moment: Earth. Not a very useful answer though
JaredPar
Ummm...it is useful if I had asked, "What planet am I on". This is the answer, and it is certainly useful.
Ed Swangren
The comments are probably because I initially just said "Simple. Don't start it twice", and that was all. (in the interests of answering quickly)... Then I edited and explained properly
Orion Edwards
Pretty soon, we'll all have to start answering with just the first letter, then edit :)
Doug L.
I'm glad I am not the only who has answered briefly first then edited to answer fully. Maybe we should just post, "I know, I know!" to start with. :)
pipTheGeek
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.
Thies
+10  A: 

Create a new BackgroundWorker object for each operation that you want to perform. E.g., 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);
}
fatcat1111
A: 

Hi all, somebody know can i kill this BackgroundWorker? i tried backgroundWorker1.CancelAsync() but it doesn't work.

Igor

Igor
I could be wrong, but you can try delete the reference to it. Then i think the carbage collector will kill it. I don't know for sure.
radbyx
Its like the blind leading the blind...
Spence
A: 

I would look into queue'ing the tasks that need to be done. You get the following advantages;

  • You do not have to handle the problems of background tasks not being able to start due to something else already running
  • You do not have to worry about using up too many threads, by creating a new background worked for each task.
  • Lastly the queue could allow you to ensure that the background tasks run in the same order as they where created/requested.

Here is an example implementation: http://thevalerios.net/matt/2008/05/a-queued-backgroundworker. I am not sure if the implementation in threadsafe, and I will update my answer once I figure out of my current locking problem in a implementation I am working with.

Thies