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
IsBusyproperty, so just change this code:worker.RunWorkerAsync();to this:
if( !worker.IsBusy ) worker.RunWorkerAsync(); else MessageBox.Show("Can't run the worker twice!");Bill K : 100% correct, 0% useful, 60% funny = +1Ed Swangren : Ummm...how does 100% correct == 0% useful?JaredPar : @Ed, I can tell you with 100% accuracy where you are at this moment: Earth. Not a very useful answer thoughEd Swangren : Ummm...it is useful if I had asked, "What planet am I on". This is the answer, and it is certainly useful.Orion Edwards : 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 properlyDoug L. : Pretty soon, we'll all have to start answering with just the first letter, then edit :)pipTheGeek : 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. :)Thies : 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. -
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
radbyx : 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.Spence : Its like the blind leading the blind... -
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.
0 comments:
Post a Comment