问题
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