protected void OnProgressChanged(ProgressEventArgs e) { if (this.ProgressChanged != null) { this.ProgressChanged(this, e); } }
/// <summary> /// Starts the video download. /// </summary> /// <exception cref="IOException">The video file could not be saved.</exception> /// <exception cref="WebException">An error occured while downloading the video.</exception> public override void Execute() { this.OnDownloadStarted(EventArgs.Empty); var request = (HttpWebRequest)WebRequest.Create(this.Video.DownloadUrl); if (this.BytesToDownload.HasValue) { request.AddRange(0, this.BytesToDownload.Value - 1); } // Additional information: The remote server returned an error: (403) Forbidden. // the following code is alternative, you may implement the function after your needs using (WebResponse response = request.GetResponse()) { using (Stream source = response.GetResponseStream()) { using (FileStream target = File.Open(this.SavePath , FileMode.Create, FileAccess.Write)) { var buffer = new byte[1024]; bool cancel = false; int bytes; int copiedBytes = 0; while (!cancel && (bytes = source.Read(buffer, 0, buffer.Length)) > 0) { target.Write(buffer, 0, bytes); copiedBytes += bytes; var eventArgs = new ProgressEventArgs((copiedBytes * 1.0 / response.ContentLength) * 100); if (this.DownloadProgressChanged != null) { this.DownloadProgressChanged(this, eventArgs); if (eventArgs.Cancel) { cancel = true; } } } } } } this.OnDownloadFinished(EventArgs.Empty); }
/// <summary> /// Starts the video download. /// </summary> /// <exception cref="IOException">The video file could not be saved.</exception> /// <exception cref="WebException">An error occured while downloading the video.</exception> public override void Execute() { // We need a handle to keep the method synchronously var handle = new ManualResetEvent(false); var client = new WebClient(); bool isCanceled = false; client.DownloadFileCompleted += (sender, args) => { handle.Close(); client.Dispose(); // DownloadFileAsync passes the exception to the DownloadFileCompleted event, if one occurs if (args.Error != null && !args.Cancelled) { throw args.Error; } handle.Set(); }; client.DownloadProgressChanged += (sender, args) => { var progressArgs = new ProgressEventArgs(args.ProgressPercentage); // Backwards compatibility this.OnProgressChanged(progressArgs); if (this.DownloadProgressChanged != null) { this.DownloadProgressChanged(this, progressArgs); if (progressArgs.Cancel && !isCanceled) { isCanceled = true; client.CancelAsync(); } } }; this.OnDownloadStarted(EventArgs.Empty); client.DownloadFileAsync(new Uri(this.Video.DownloadUrl), this.SavePath); handle.WaitOne(); this.OnDownloadFinished(EventArgs.Empty); }
private void VideoDownloader_DownloadProgressChanged(object sender, ProgressEventArgs e) { DownloadProgress?.Invoke(this, e); }
/// <summary> /// Updates download progress /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void videoDownloader_DownloadProgressChanged(object sender, ProgressEventArgs e) { if (!cancelDownload) { DownloadProgress = (int)e.ProgressPercentage; } else { e.Cancel = true; DownloadProgress = 0; } }