Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient DownloadFileAsync hangs

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.

like image 667
mxpv Avatar asked Nov 24 '11 11:11

mxpv


1 Answers

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

like image 88
Simon Avatar answered Oct 22 '22 10:10

Simon