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
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
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!");
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);
}
Hi all, somebody know can i kill this BackgroundWorker? i tried backgroundWorker1.CancelAsync() but it doesn't work.
Igor
I would look into queue'ing the tasks that need to be done. You get the following advantages;
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.