private void DeleteDownload(Model.Download download) { download.ResetDownloadPart(); download.TotalFileSize = 0; DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); }
private void DownloadServiceOnDownloadCancel(object sender, MyDownloadEventArgs myDownloadEventArgs) { Download.State = CurrentDownloadState.Cancel; Debug.WriteLine("Download cancel"); DownloadCancel?.Invoke(sender, myDownloadEventArgs); }
private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs) { if (asyncCompletedEventArgs.Cancelled == true) { DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); } else { DownloadComplete?.Invoke(this, new MyDownloadEventArgs()); } }
private async Task RunDownload(Model.Download download, Action <HttpWebRequest> configureRequest, FileRenameStreamer fileRenameStreamer) { try { _cancellationTokenSource = new CancellationTokenSource(); var request = (HttpWebRequest)WebRequest.Create(new Uri(download.SourcePath)); configureRequest(request); using (var response = await request.GetResponseAsync()) { fileRenameStreamer.SetContentLength(response.ContentLength); var data = response.GetResponseStream(); using (fileRenameStreamer) { var byteBuffer = fileRenameStreamer.GetByteBuffer(); fileRenameStreamer.SetStreamPosition(download.GetBytesFromAllParts()); download.TotalFileSize = fileRenameStreamer.GetTotalFileSize(); await MakeDownload(download, data, byteBuffer, _cancellationTokenSource.Token, fileRenameStreamer); } } } catch (IOException) { DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); } catch (OperationCanceledException) { if (download.State == CurrentDownloadState.Cancel) { DeleteDownload(download); } else if (download.State == CurrentDownloadState.Pause) { DownloadPause?.Invoke(this, new MyDownloadEventArgs()); } else { DeleteDownload(download); } } catch (WebException) { DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); } }
public async void DownloadFile(Model.Download download) { try { using (_webClient = new WebClient()) { _webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted; _webClient.DownloadProgressChanged += WebClientOnDownloadProgressChanged; await _webClient.DownloadFileTaskAsync(new Uri(download.SourcePath), download.TargetPathWithFileName); } } catch (Exception e) { DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); } }