Aborting a WebClient.DownloadFileAsync operation


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.


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)



I'm not sure why you would get an exception from calling CancelAsync.

I use WebClient to handle paralell downloads in our current project, and upon calling CancelAsync the event DownloadFileCompleted is raised by WebClient, where the property Cancelled is true. My event handler looks like this:

private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) { this.CleanUp(); // Method that disposes the client and unhooks events return; } if (e.Error != null) // We have an error! Retry a few times, then abort. { if (this.retryCount < RetryMaxCount) { this.retryCount++; this.CleanUp(); this.Start(); } // The re-tries have failed, abort download. this.CleanUp(); this.errorMessage = "Downloading " + this.fileName + " failed."; this.RaisePropertyChanged("ErrorMessage"); return; } this.message = "Downloading " + this.fileName + " complete!"; this.RaisePropertyChanged("Message"); this.progress = 0; this.CleanUp(); this.RaisePropertyChanged("DownloadCompleted"); }

And the cancelling method is simply:

/// <summary> /// If downloading, cancels a download in progress. /// </summary> public virtual void Cancel() { if (this.client != null) { this.client.CancelAsync(); } }


  • Java Socket Reuse
  • Socket.io takes a long time before triggering the disconnect event
  • How WinForm and uwp communicate in two directions
  • Erlang raise number of opened sockets, MacOS and CentOs
  • How do I add a File Type Association in a Windows Phone 8.1 app manifest?
  • Unzip archive in .Net CORE 1.0
  • Available space left on drive - WinAPI - Windows CE
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Where these are stored?
  • Translating C# to PowerShell in InterIMAP
  • Display images in Django
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Very simple C++ DLL that can be called from .net
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • ilmerge with a PFX file
  • Fetching methods from BroadcastReceiver to update UI
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • javascript inside java/jsp code
  • Validaiting emails with Net.Mail MailAddress
  • Symfony2: How to get request parameter
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Do I've to free mysql result after storing it?
  • GridView Sorting works once only
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Android Studio and gradle
  • Proper folder structure for lots of source files
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • WPF Applying a trigger on binding failure
  • Load html files in TinyMce
  • How do I configure my settings file to work with unit tests?
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Busy indicator not showing up in wpf window [duplicate]
  • How can i traverse a binary tree from right to left in java?
  • Why do underscore prefixed variables exist?