而Web客户端的下载文件异步(在C#)检测连接中断 [英] detecting connection drops while WebClient is downloading a file asynchronously (in C#)

查看:118
本文介绍了而Web客户端的下载文件异步(在C#)检测连接中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Web客户端的DownloadFileAsync(在C#)方法来异步下载文件。我有连接到DownloadProgressChanged和DownloadFileCompleted事件的事件处理程序。我希望通过在DownloadFileCompleted事件处理AsyncCompletedEventArgs的error属性得到通知的任何错误。它运作良好,如果连接是不是present下载开始前。它掷骰子了适当错误,我得到了我上面提到的属性错误。但是,如果连接中断,而下载的过程中没有任何反应。事件处理程序不被调用,它一直等下去。我应该怎么做来处理这种情况?
谢谢你。

I am using WebClient's DownloadFileAsync (in C#) method to download files asynchronously. I have event handlers attached to DownloadProgressChanged and DownloadFileCompleted events. I hoped to get notified of any errors through AsyncCompletedEventArgs's Error property in the DownloadFileCompleted event handler. It works well if the connection is not present before the download begins. It craps out with proper error and I get the error in the property I mentioned above. But if the connection drops while the download is in progress nothing happens. The event handler is not called, it keeps waiting forever. What should I do to handle such a scenario ? Thanks.

推荐答案

做你问什么是最好的方法可能是实现自己的呼叫超时。

The best way to do what you're asking is probably to implement your own timeout for the call.

基本上,创建一个定时器,获取重置每次DownloadProgressChanged事件被炒鱿鱼的时间。这样,如果你没有得到你的超时期限内的任何最新进展,你可以调用Web客户端的CancelAsync命令,这样你不等待撒手人寰。

Basically, create a timer that gets reset every time the DownloadProgressChanged event gets fired. That way, if you don't get any progress updates within your timeout period, you can call the CancelAsync command on the WebClient so you're not waiting around forever.

这是我已经能够做到这一点的唯一方法。

That's the only way I've been able to do it.

这篇关于而Web客户端的下载文件异步(在C#)检测连接中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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