private void TryCleanupExistingDownloadWebClient() { if (downloadWebClient == null) { return; } try { lock (this) { if (downloadWebClient != null) { downloadWebClient.DownloadFileCompleted -= OnDownloadCompleted; downloadWebClient.DownloadProgressChanged -= OnDownloadProgressChanged; downloadWebClient.OpenReadCompleted -= OnOpenReadCompleted; downloadWebClient.CancelAsync(); downloadWebClient.Dispose(); downloadWebClient = null; } } } catch (System.Exception) { //logger.Warn("Error while cleaning up web client : {0}", e.Message); } }
private void TriggerWebClientDownloadFileAsync() { //logger.Debug("Falling back to legacy DownloadFileAsync."); try { isFallback = true; var destinationDirectory = Path.GetDirectoryName(localFileName); if (destinationDirectory != null && !Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } TryCleanupExistingDownloadWebClient(); downloadWebClient = CreateWebClient(); downloadWebClient.DownloadFileAsync(fileSource, localFileName); //logger.Debug("Download async started. Source: {0} Destination: {1}", fileSource, localFileName); } catch (System.Exception ex) { //logger.Warn("Failed to download Source:{0}, Destination:{1}, Error:{2}.", fileSource, localFileName, ex.Message); if (!AttemptDownload()) { InvokeDownloadCompleted(CompletedState.Failed, localFileName, ex); } } }
private DownloadWebClient CreateWebClient() { var webClient = new DownloadWebClient(); webClient.DownloadFileCompleted += OnDownloadCompleted; webClient.DownloadProgressChanged += OnDownloadProgressChanged; webClient.OpenReadCompleted += OnOpenReadCompleted; return(webClient); }
private void Download(Uri source, string fileDestination, long totalBytesToReceive) { try { long seekPosition; FileUtils.TryGetFileSize(fileDestination, out seekPosition); TryCleanupExistingDownloadWebClient(); downloadWebClient = CreateWebClient(); downloadWebClient.OpenReadAsync(source, seekPosition); //logger.Debug("Download started. Source: {0} Destination: {1} Size: {2}", source, fileDestination, totalBytesToReceive); } catch (System.Exception e) { //logger.Debug("Download failed: {0}", e.Message); if (!AttemptDownload()) { InvokeDownloadCompleted(CompletedState.Failed, localFileName, e); } } }