2017-01-21 1 views
-2

私は、全体のダウンロードの進行状況を表示しprogressBar2が100%であるかどうかを確認しようとしたが、それは本当に働いていない前に。それを確認するもう1つの方法がありますか?例えばすべてのファイルがダウンロードされたかどうかを確認するにはどうすればよいですか?

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(); 
     } 

iは100件のURLを持っている場合、それは私がすべてのファイルがダウンロードされたときに完了した場合に知ってほしいとだけでなく、1時間ごと、その後ダウンロードを開始しています。

答えて

1

をサイズを取得するforeachファイルを試してみて、totalBytesToDownloadにそれを合計して、プログレスバーに

を、それを使用しない理由は、その後、あなたはどのように追跡することができます多くのダウンロードがカウンタ変数で完了しています。そのためカウンタにアクセスすることができ、複数のスレッドで、そのカウンターを操作するInterlockedクラスを使用します。

これはあなたのコードに必要な変更されている:我々が行われた場合、カウンタの取り扱いとチェックここ

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(); 
} 

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

私は非常によくそれを理解したが、何を達成しようとしていることはあなたのプログレスバーは、すべてのファイルではなく、一つのファイルは、その後リセットし、その後、別のファイルをリセットし、ダウンロードの進行状況を示しているということですしていません。

その場合は、あなたがそう

foreach(string url in urll) 
{ 
    //get url file size 
    totalBytesToDownload = totalBytesToDownload + urlSizeYouGot; 
} 
関連する問題