public async void ResumeDownload(Model.Download download) { download.FileRenameCancelToken = new FileRenameCancelToken(); var fileResume = new FileResumeStreamer(download.TempFileName, download.TargetPathWithFileName, download.FileRenameCancelToken); await RunDownload( download, request => request.AddRange(download.GetBytesFromAllParts()), fileResume); }
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()); } }
private static double SendDownloadProgress(IProgress <MyProgress> onProgressChanged, Model.Download download, double oldPercentDone) { var myProgress = new MyProgress() { CurrentFileSize = download.GetBytesFromAllParts(), TotalFileSize = download.TotalFileSize }; var percentDone = (double)myProgress.CurrentFileSize / myProgress.TotalFileSize; if (!(percentDone - oldPercentDone > 0.05)) { return(oldPercentDone); } onProgressChanged?.Report(myProgress); return(percentDone); }