中止WebClient.DownloadFileAsync操作 [英] Aborting a WebClient.DownloadFileAsync operation

查看:392
本文介绍了中止WebClient.DownloadFileAsync操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是安全地取消DownloadFileAsync操作的最佳方式?



我有一个线程(后台工作)揭开序幕的下载和管理它的其他方面,我结束时,我看到线程 CancellationPending ==真。拉开下载后,该线程将坐在旋转,直至下载完成,或者线程被取消。



如果该线程将被取消,我想取消下载。是否有这样一个标准的成语?我试过 CancelAsync ,但我得到它(中止)引发WebException。 。我不知道这是在做取消的清洁方式。



感谢



编辑:在第一种例外情况是,并设置在一个对内部流对象(调用堆栈):




System.dll中System.Net.Sockets.NetworkStream。 EndRead(System.IAsyncResult asyncResult)
System.dll中!System.Net.PooledStream.EndRead(System.IAsyncResult asyncResult)



解决方案

我不知道你为什么会从调用CancelAsync得到一个异常。



我使用Web客户端来处理paralell下载我们的目前的项目,并在调用CancelAsync DownloadFileCompleted 是由Web客户端,其中取消属性是真的引发的事件。我的事件处理程序是这样的:

 私人无效OnDownloadFileCompleted(对象发件人,AsyncCompletedEventArgs E)
{
如果(e.Cancelled)
{
this.CleanUp(); //即部署在客户端和unhooks事件
回报率的方法;
}

如果(e.Error!= NULL)//我们有一个错误!重试几次,然后终止。
{
如果(this.retryCount< RetryMaxCount)
{
this.retryCount ++;
this.CleanUp();
this.Start();
}

//重新尝试已经失败,中止下载。
this.CleanUp();
this.errorMessage =下载+ this.fileName +失败。
this.RaisePropertyChanged(的ErrorMessage);
的回报;
}

this.message =下载+ this.fileName +完成!;
this.RaisePropertyChanged(信息);

this.progress = 0;

this.CleanUp();
this.RaisePropertyChanged(DownloadCompleted);
}

和的消除方法很简单:

  ///<总结> 
///如果下载,取消正在进行的下载。
///< /总结>
公共虚拟无效取消()
{
如果(this.client!= NULL)
{
this.client.CancelAsync();
}
}


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)

解决方案

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();
    }
}

这篇关于中止WebClient.DownloadFileAsync操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆