I have a background worker that does basically the following:
- Find next available file and mark it as in process
- Process the file and save the updated version as a new file
- Mark the original as processed
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
WorkerSupportsCancellationto true, and periodically check theCancellationPendingproperty in theDoWorkevent handler.The
CancelAsyncmethod only sets theCancellationPendingproperty. It doesn’t kill the thread; it’s up to the worker to respond to the cancellation request.e.g.: