WebClient DownloadFileAsync hangs

南笙酒味 提交于 2020-01-03 10:45:41

问题


Good day. I'm working on file downloader class using DownloadFileAsync. In normal situations everything works fine. But when I'm downloading file and disable network connection, downloading progress is just stops for infinite time, without raising any errors or calling any callbacks. Any ideas how to handle this situation? Many thanks.

_client.Proxy = WebRequest.DefaultWebProxy;
_client.DownloadProgressChanged += (sender, argv) => { actionCallback(argv.ProgressPercentage); }
_client.DownloadFileCompleted += (sender, argv) => { 
      if (argv.Error != null) { 
          _exc = argv.Error;
      } 
      set event
}
 Task.Factory.StartNew(() => {
     _client.DownloadFileAsync(request, targetFileName);

     thread sync

     if (_exc != null) {
         logger.ErrorException(exc);
         throw;
     }

The problem appears under Vista and 2k8. On Win7 everything is ok.


回答1:


Add an event handler to the WebClient.OpenReadCompleted event. The OpenReadCompletedEventArgs has a boolean property for Error if an error occurred.



来源:https://stackoverflow.com/questions/8256118/webclient-downloadfileasync-hangs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!