I was using Background worker at the user control level and it was working fine.But then I pushed the worker in one of the Helper function.My question is that it works fine until i try to cancel the task. In the DoWork function there is loop calling the web service each time and before calling the service it check cancellation pending and if yes then it should break the loop.
1) Make sure that you have set the WorkerSupportsCancellation
property on your BackgroundWorker
to True
.
2) Make sure that you are properly disposing any web service reference. (Just because it's what you should be doing anyway)
You may also want to step through the BackgroundWorker and verify that when cancelled, the cancellation pending property is being set to true.
If none of these things help, then you might want to consider showing the code for creating and starting your backgroundworker, as well as any code for the events (DoWork, RunWorkerCompleted, etc).
EDIT (this got removed when Will performed an edit)
You may also want to check out this link.
An imporant thing to get from that link is that even when you cancel, if the worker is already doing work (IE: processing through DoWork) it can miss the cancellationpending and continue it's work.