2017-01-21 3 views
-2

Bevor ich versuchte, zu überprüfen, ob progressBar2, die den Fortschritt Gesamt Download zeigen bei 100%, aber es ist nicht wirklich funktioniert. Gibt es einen sichereren Weg, es zu überprüfen?Wie kann ich prüfen, ob alle Dateien heruntergeladen wurden?

private void btnDownload_Click(object sender, EventArgs e) 
     { 
      //urll.Add("http://download.thinkbroadband.com/1GB.zip"); 
      btnDownload.Enabled = false; 
      label7.Text = "Downloading..."; 
      getTotalBytes(countryList); 
      CreateCountryDateTimeDirectories(newList); 
      downloadFile(newList); 
     } 

     private Queue<string> _downloadUrls = new Queue<string>(); 

     private async void downloadFile(IEnumerable<string> urls) 
     { 
      foreach (var url in urls) 
      { 
       _downloadUrls.Enqueue(url); 
      } 

      await DownloadFile(); 
     } 

     private async Task DownloadFile() 
     { 
      if (_downloadUrls.Any()) 
      { 
       WebClient client = new WebClient(); 
       client.DownloadProgressChanged += ProgressChanged; 
       client.DownloadFileCompleted += Completed; 

       var url = _downloadUrls.Dequeue(); 

       sw = Stopwatch.StartNew(); 

       if (url.Contains("true")) 
       { 
        await client.DownloadFileTaskAsync(new Uri(url), countriesMainPath + "\\" + currentDownloadCountry + "\\" + count + "Infrared.jpg"); 
       } 
       else 
       { 
        await client.DownloadFileTaskAsync(new Uri(url), countriesMainPath + "\\" + currentDownloadCountry + "\\" + count + "Invisible.jpg"); 
       } 

       return; 
      } 
     } 

     double percentageTotalDownload = 0; 
     double totalBytesDownloaded = 0; 
     private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      // Calculate download speed and output it to labelSpeed. 
      label3.Text = string.Format("{0} kb/s", (e.BytesReceived/1024d/sw.Elapsed.TotalSeconds).ToString("0.00")); 

      // Update the progressbar percentage only when the value is not the same. 
      double bytesInCurrentDownload = (double)e.BytesReceived; 
      double totalBytesCurrentDownload = double.Parse(e.TotalBytesToReceive.ToString()); 
      double percentageCurrentDownload = bytesInCurrentDownload/totalBytesCurrentDownload * 100; 
      ProgressBar1.Value = int.Parse(Math.Truncate(percentageCurrentDownload).ToString());//e.ProgressPercentage; 
                           // Show the percentage on our label. 
      Label4.Text = e.ProgressPercentage.ToString() + "%"; 

      // Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading 
      label10.Text = string.Format("{0} MB's/{1} MB's", 
       (e.BytesReceived/1024d/1024d).ToString("0.00"), 
       (e.TotalBytesToReceive/1024d/1024d).ToString("0.00")); 

      //Let's update ProgressBar2 
      totalBytesDownloaded = e.BytesReceived + bytesFromCompletedFiles; 
      percentageTotalDownload = totalBytesDownloaded/totalBytesToDownload * 100; 
      progressBar2.Value = (int)percentageTotalDownload; 
      label6.Text = progressBar2.Value.ToString() + "%"; 
     } 

     long bytesFromCompletedFiles = 0; 
     // The event that will trigger when the WebClient is completed 
     private async void Completed(object sender, AsyncCompletedEventArgs e) 
     { 
      await DownloadFile(); 
     } 

Zum Beispiel, wenn ich 100 Urls und es starten Sie den Download dann möchte ich in der fertiggestellten Ereignis wissen, wenn alle Dateien heruntergeladen und nicht nur ein jedes Mal.

Antwort

1

Sie können verfolgen, wie Viele Downloads wurden in einer Counter-Variable abgeschlossen. Wegen der mehreren Threads, die diesen Zähler zugreifen kann, verwenden, um die Klasse, die Interlocked Zähler zu manipulieren.

Dies sind die in Ihrem Code erforderlich Änderungen:

private int urlCount = 0; // keep track of how many urls are processed 

private async void downloadFile(IEnumerable<string> urls) 
{ 
    urlCount = 0; 
    foreach (var url in urls) 
    { 
     _downloadUrls.Enqueue(url); 
     urlCount++; 
    } 
    // urlCount is now set 
    await DownloadFile(); 
} 

Und hier ist die Handhabung des Zählers und die Prüfung, wenn wir fertig sind

private async void Completed(object sender, AsyncCompletedEventArgs e) 
{ 
    // urlCount will be decremented 
    // cnt will get its value 
    var cnt = System.Threading.Interlocked.Decrement(ref urlCount); 

    if (cnt > 0) { 
     await DownloadFile(); 
    } 
    else 
    { 
     // call here what ever you want to happen when everything is 
     // downloaded 
     "Done".Dump(); 
    } 
} 
-1

Ich habe es nicht sehr gut verstanden, aber was Sie versuchen zu erreichen, dass Ihre Fortschrittsbalken Fortschritt der Download zeigt alle Dateien und nicht eine, dann eine andere Datei zurückgesetzt Datei dann, die zurückgesetzt.

Wenn das der Fall ist, dann warum Sie nicht versuchen, foreach Dateigröße erhalten und es zu totalBytesToDownload zusammenzufassen und verwenden Sie es dann in Fortschrittsbalken

So

foreach(string url in urll) 
{ 
    //get url file size 
    totalBytesToDownload = totalBytesToDownload + urlSizeYouGot; 
} 
Verwandte Themen