I have a time taking process in DoWork
of my BackgroundWorker
.
Whenever I try to Cancel the job by backgroundWorker1.CancelAsync()
, the backgroundWorker1.CancellationPending
becomes Pending and I should wait for the next iteration in my DoWork
to Cancel the job and stepping out of it myself.
Is there any way to Cancel the job immediately after calling it?
No. In general you can't safely terminate a thread "immediately", because it may be holding resources that would leak, and more importantly it may be holding locks. You need to structure your worker to respect the cancellation flag and and safely exit as soon as possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With