How to cancel BackgroundWorker immediately?

I have a process running time in DoWork my BackgroundWorker .

Whenever I try to cancel a task on backgroundWorker1.CancelAsync() , backgroundWorker1.CancellationPending becomes pending, and I have to wait for the next iteration in my DoWork to cancel the task and exit it myself.

Is there a way to cancel a task right after it is called?

+6
source share
1 answer

Not. In general, you cannot safely terminate a thread "immediately" because it could be a resource leak, and, more importantly, it might contain locks. You need to structure your employee to comply with the cancellation flag and exit safely as soon as possible.

+7
source

Source: https://habr.com/ru/post/908068/


All Articles