I have a background worker that does basically the following:
The above steps will need to loop and continue processing while there are files to process.
I would like the Background Worker to be able to be stopped, and I see the WorkerSupportsCancellation setting, but how do I ensure that it can only stop between files, not while a file is being processed?
Set WorkerSupportsCancellation
to true, and periodically check the CancellationPending
property in the DoWork
event handler.
The CancelAsync
method only sets the CancellationPending
property. It doesn't kill the thread; it's up to the worker to respond to the cancellation request.
e.g.:
private void myBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
while( !myBackgroundWorker.CancellationPending )
{
// Process another file
}
}
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