I'm not sure why you got an exception from calling CancelAsync.
I use WebClient to handle Paralell downloads in our current project, and when CancelAsync is called, the event DownloadFileCompletedis generated by WebClient, where the property Cancelledis true. My event handler looks like this:
private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
this.CleanUp();
return;
}
if (e.Error != null)
{
if (this.retryCount < RetryMaxCount)
{
this.retryCount++;
this.CleanUp();
this.Start();
}
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 cancellation method is simple:
public virtual void Cancel()
{
if (this.client != null)
{
this.client.CancelAsync();
}
}
source
share