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 static void OnDownloadPause(object sender, ProgressEventArgs ev) => DownloadPause?.Invoke(sender, ev);
private void DownloadServiceOnDownloadPause(object sender, MyDownloadEventArgs myDownloadEventArgs) { DownloadPause?.Invoke(sender, myDownloadEventArgs); }