What is the best way to cancel a DownloadFileAsync operation safely?
I have a thread (background worker) that kicks off the download and manages other aspects of it and that I end when I see that the thread has CancellationPending == true. After kicking off the download, the thread will sit and spin until the download has completed, or the thread is cancelled.
If the thread is cancelled, I want to cancel the download. Is there a standard idiom for doing this? I've tried CancelAsync, but I get a WebException from it (aborted). I'm not sure this is a clean way of doing the cancel.
Thanks.
Edit: the first exception is and object disposed one on the internal stream (call stack):
System.dll!System.Net.Sockets.NetworkStream.EndRead(System.IAsyncResult asyncResult) System.dll!System.Net.PooledStream.EndRead(System.IAsyncResult asyncResult)