Say I have a form that is performing a bulk file copy. I have written an async method to do this work, and have successfully implemented a cancellation process, using a cancellation token. Part of the cancellation is to delete all the files currently copied, prior to leaving the Task.
This works fine if the user clicks the Cancel button. However in cases where the user closes the form, I cannot find a way to wait on the underlying Task before progressing with the rest of the Form closing procedure. As such, the Task is terminated, and the clean up does not occur.
Can anyone recommend a pattern for preventing the form from closing prior to all awaited threads completing?