Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Net;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7.  
  8. namespace WindowsFormsApplication1
  9. {
  10.     class Installer
  11.     {
  12.         Button saveButton;
  13.         ProgressBar progressBar;
  14.         Label statusLabel;
  15.         Boolean downloadDone;
  16.  
  17.         public Installer(Button _saveButton, ProgressBar _progressBar, Label _statusLabel)
  18.         {
  19.             saveButton = _saveButton;
  20.             progressBar = _progressBar;
  21.             statusLabel = _statusLabel;
  22.         }
  23.  
  24.         public async void Start(string fileUrl)
  25.         {
  26.             saveButton.BeginInvoke((Action)(() => {
  27.                 saveButton.Enabled = false;
  28.             }));
  29.  
  30.             Task<bool> downloadArchiveTask = DownloadArchiveAsync(fileUrl);
  31.  
  32.             bool downloadArchiveDone = await downloadArchiveTask;
  33.  
  34.             if (downloadArchiveDone)
  35.                 statusLabel.BeginInvoke((Action)(() => {
  36.                     statusLabel.Text = "Download Completed";
  37.                 }));
  38.         }
  39.  
  40.         async Task<bool> DownloadArchiveAsync(string fileUrl)
  41.         {
  42.             var downloadLink = new Uri(fileUrl);
  43.             var saveFilename = Path.GetFileName(downloadLink.AbsolutePath);
  44.  
  45.             DownloadProgressChangedEventHandler DownloadProgressChangedEvent = (s, e) =>
  46.             {
  47.                 progressBar.BeginInvoke((Action)(() => {
  48.                     progressBar.Value = e.ProgressPercentage;
  49.                 }));
  50.  
  51.                 var downloadProgress = string.Format("{0} MB / {1} MB",
  52.                         (e.BytesReceived / 1024d / 1024d).ToString("0.00"),
  53.                         (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
  54.  
  55.                 statusLabel.BeginInvoke((Action)(() => {
  56.                     statusLabel.Text = "Downloading " + downloadProgress + " ...";
  57.                 }));
  58.             };
  59.  
  60.             AsyncCompletedEventHandler AsyncCompletedEvent = (s, e) =>
  61.             {
  62.                 // Todo: Extract
  63.                 downloadDone = true;
  64.             };
  65.  
  66.             using (WebClient webClient = new WebClient())
  67.             {
  68.                 webClient.DownloadProgressChanged += DownloadProgressChangedEvent;
  69.                 webClient.DownloadFileCompleted += AsyncCompletedEvent;
  70.                 webClient.DownloadFileAsync(downloadLink, saveFilename);
  71.             }
  72.  
  73.             await IsDownloadDone();
  74.  
  75.             return true;
  76.         }
  77.  
  78.         async Task IsDownloadDone()
  79.         {
  80.             await Task.Run(() =>
  81.             {
  82.                 while (!downloadDone) ;
  83.             });
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement