cancel Background Worker immediately

liptonIcedTea

Well-known member
Joined
Jan 18, 2007
Messages
89
Programming Experience
1-3
Hi,

I'm using Backgroundworker to start a thread which goes off and generates a report using some complex calculations.

These reports can take a long time to generate (over an hour at times) and I would like the ability to cancel the report if I wished.

Backgroundworker offers a CancelAsync method. This sets a property inside Backgroundworker, and it is up to you to programmatically stop the thread inside your doWork method.

Now I do not understand why Microsoft has implemented like this... because although this works only if the bulk of your calculations go around a loop. If however, the bulk of my calculations is inside a method in which you have no access to, CancelASync is useless.

Is there a way to destroy a Backgroundworker thread immediately? I have tried Dispose(), that does not seem to work.

Thank you
 
If the BackgroundWorker component is not suitable for your needs then you shouldn't be using it. The BackgroundWorker provides an easy wrapper for simple multi-threading functionality but, as usual, with ease of use comes a decrease in power and flexibility.

You should use explicit multi-threading, in which case you can call the Abort method of your Thread object and handle the ThreadAbortException is generates. You'll find this to be messier and more fiddly than doing things the BackgroundWorker way and THAT is the reason Microsoft chose to do it that way.

If you want to use a BackgroundWorker then it's up to you to structure your code such that it will work the way you need it to. If this is your method then you can have it take a BackgroundWorker as a parameter, in which case you do have access to the CancellationPending property. If you can't do that then a BackgroundWorker is simply not suitable. No-one promised that it would be for all situations.
 
Thread.Abort is the only way if you can't control the "other" method that is working, but BackgroundWorkers approach is recommended way of doing cancellable asynchronous methods. Thread.Abort is unpredictable, for example if the external method spawns threads of its own they will not stop even if you stop the thread that runs main method. When you stop a method in the middle of its work you also prevent it from doing perhaps needed cleaning up, which may include memory leaks, locked file handles, orphaned files in file system etc.
 
Thank you guys for the quick reply.

I will have to decide on how to go about this. I don't want to use explicit threading, because I really want my code to be as understandable as possible... I was hoping someone would've thought of a work around...
 
It does "Thread.Abort" for you, so it's valid in the same cases.
 
yes, it pretty much just extends upon Backgroundworker to expose the Abort method to the user.

The only thing is using Abort throws AbortThreadExceptions and I've had to go around catching these exceptions and just ignoring them... and i'm not a big fan of doing that.

But u know, sometimes the best practice isn't always the best solution, so if it works, I guess I'm just to leave it like this.
 
Back
Top