Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.66 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. One progressbar vs. many webclients
  2. foreach (string line in textBox3.Lines)
  3.                     {
  4.                         int pos = line.IndexOf("?v=");
  5.                         string videoid = line.Substring(pos + 3, 11);
  6.                         GetFile(videoid);
  7.                     }
  8.  
  9.         GetFile() {
  10.         ...code
  11.  
  12.         WebClient webClient = new WebClient();
  13.         webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  14.         webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  15.         webClient.DownloadFileAsync(new Uri(fileRequest), @textBox2.Text + @"" + title + ".mp3");
  16.     }
  17.  
  18.     private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  19.     {
  20.         progressBar1.Value = e.ProgressPercentage;
  21.     }
  22.        
  23. public class WebClientProgressManager : INotifyPropertyChanged
  24.         {
  25.             private readonly Dictionary<WebClient,int> _clients = new Dictionary<WebClient, int>();
  26.             private const string TotalProgressPropertyName = "TotalProgress";
  27.             public void Add(WebClient client)
  28.             {
  29.                 if (client == null)
  30.                     throw new ArgumentNullException("client");
  31.                 if (_clients.ContainsKey(client)) return;
  32.  
  33.                 client.DownloadProgressChanged += (s, e) =>
  34.                                                       {
  35.                                                           if (e.ProgressPercentage == 100)
  36.                                                           {
  37.                                                               _clients.Remove((WebClient)s);
  38.                                                           }
  39.                                                           RaisePropertyChanged(TotalProgressPropertyName);
  40.                                                       };
  41.                 _clients.Add(client,0);
  42.  
  43.             }
  44.  
  45.             private void RaisePropertyChanged(string propertyName)
  46.             {
  47.                 if (PropertyChanged != null)
  48.                 {
  49.                     PropertyChanged.Invoke(this,new PropertyChangedEventArgs(propertyName));
  50.                 }
  51.             }
  52.  
  53.             public int TotalProgress
  54.             {
  55.  
  56.                 get
  57.                 {
  58.                     if (_clients.Count == 0) return 100; //need something here to prevent divide-by-zero
  59.                     int progress = _clients.Sum(client => client.Value);
  60.                     return progress/_clients.Count;
  61.                 }
  62.             }
  63.  
  64.             #region Implementation of INotifyPropertyChanged
  65.  
  66.             public event PropertyChangedEventHandler PropertyChanged;
  67.  
  68.             #endregion
  69.         }