If I want to periodically check if there is a revocation request, I would constantly use the following code below inside my DoWork event handler:
if(w.CancellationPending == true) { e.Cancel = true; return; }
Is there a clean way to test the cancellation request in BackgroundWorker in C # without re-entering the same code again and again?
Please see the following code below:
void worker_DoWork(object sender, DoWorkEventArgs e) { ... BackgroundWorker w = sender as BackgroundWorker; if(w.CancellationPending == true) { e.Cancel = true; return; } some_time_consuming_task... if(w.CancellationPending == true) { e.Cancel = true; return; } another_time_consuming_task... if(w.CancellationPending == true) { e.Cancel = true; return; } ... }
source share