我不知道爲什麼你會從調用CancelAsync中得到異常。
我使用WebClient處理當前項目中的並列下載,並且在調用CancelAsync時,由WebClient引發事件DownloadFileCompleted
,其中屬性Cancelled
爲真。我的事件處理程序是這樣的:
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");
}
與所述消除方法很簡單:
/// <summary>
/// If downloading, cancels a download in progress.
/// </summary>
public virtual void Cancel()
{
if (this.client != null)
{
this.client.CancelAsync();
}
}
感謝。我已經更新了問題以顯示我正在獲取的內容(現在,我首先獲得了一個對象處置異常,似乎來自底層流)。也許我是在等待完成取消?我會調查。 – Robinson 2012-04-26 12:42:20
@Robinson也許這可能會引起人們的興趣:「如果文件足夠快地下載進度變化事件仍然在關閉文件之後,WebClient就會拋出,捕獲並忽略Object Disposed異常。」 (參考:http://stackoverflow.com/questions/3169749/system-net-webclient-cancelasync-throws-objectdisposedexception-cannot-acces) – 2012-04-26 12:56:29
@Robinson你也應該注意到CancelAsync並不立即取消下載,所以如果你例如過早地處理對象,他們可能會得到一個Object Disposed異常,因爲該進程還沒有被取消。 – 2012-04-26 13:00:55