2017-01-22 42 views
0

我有一个按钮单击事件,我做一次,一些列表,并启动backgroundworker1:我如何确保100%背景工作者不忙,然后再次启动它?

private void btnDownload_Click(object sender, EventArgs e) 
     { 
      btnDownload.Enabled = false; 
      label7.Text = "Downloading..."; 
      ei.Init(); 
      if (countryList.Count() == 0) 
      { 
       foreach (ExtractImages.Continent continent in ei.world.continents) 
       { 
        foreach (ExtractImages.Country country in continent.countries) 
        { 
         if (country.name == "Israel") 
         { 
          foreach (string imageUri in country.imageUrls) 
          { 
           countryList.Add(imageUri); 
          } 
         } 
         else 
         { 
          foreach (string imageUri in country.imageUrls) 
          { 
           newList.Add(imageUri); 
          } 
         } 
        } 
       } 
      } 
      backgroundWorker1.RunWorkerAsync(); 
     } 

在DoWork的事件:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 

      if (downloaded == false) 
      { 
       getTotalBytes(countryList); 
       CreateCountryDateTimeDirectories(countryList); 
       downloadFile(countryList); 
      } 
      else 
      { 
       getTotalBytes(newList); 
       CreateCountryDateTimeDirectories(newList); 
       downloadFile(newList); 
      } 
     } 

在DoWork的事件中,我有两个阶段首先,它将使计算并将下载列表countryList中的链接。

我想要做的就是一旦完成再次下载countryLinks start backgroundworker1中的所有链接并且这次下载列表newList中的链接。

这是我如何下载列表中的链接。

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

     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 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), @"c:\temp\TempSatFiles\" + urlCount + "Infrared.jpg"); 
        //await client.DownloadFileTaskAsync(new Uri(url), countriesMainPath + "\\" + currentDownloadCountry + "\\" + urlCount + "Infrared.jpg"); 
       } 
       else 
       { 
        await client.DownloadFileTaskAsync(new Uri(url), @"c:\temp\TempSatFiles\" + urlCount + "Invisible.jpg"); 
        //await client.DownloadFileTaskAsync(new Uri(url), countriesMainPath + "\\" + currentDownloadCountry + "\\" + urlCount + "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; 
     private async void Completed(object sender, AsyncCompletedEventArgs e) 
     { 
      var cnt = System.Threading.Interlocked.Decrement(ref urlCount); 

      if (cnt > 0) 
      { 
       await DownloadFile(); 
       label9.Text = urlCount.ToString(); 
      } 
      else 
      { 
       label7.Text = "Download completed"; 
       downloaded = true; 
       btnDownload.Enabled = true; 
       sw.Stop(); 
      } 
     } 

现在它会下载countryList中的链接。 当完成下载列表中的所有文件也将获得else部分:这次在DoWork的事件,将下载的newList链接

label7.Text = "Download completed"; 
downloaded = true; 
btnDownload.Enabled = true; 
sw.Stop(); 

在这里,我要重新启动backgroundworker1和。

问题是我怎么知道backgroundworker1不忙?有可能是它下载了所有文件,并且backgroundworker1仍然很忙?

或者我应该在backgroundworker完成的事件中启动背景工作?如果它完成了所有的下载,它将会到达背景完成的事件?或者首先它会到达webclient完成的事件?

+0

管理多个'BackgroundWorker's总是很尴尬;这是您的代码已准备好[从BGW升级到'Task.Run']的标志之一(http://blog.stephencleary.com/2013/05/taskrun-vs-backgroundworker-intro.html)。 –

回答

1

您可以检查如果工人是使用忙或不...

if(!backgroundworker1 .IsBusy) 
    backgroundworker1 .RunWorkerAsync(); 

没有,如果你当它完成,然后工人将完成下载完成时就已经适当的检查,它可以当它等待回应并且回应不会很快到来时,它会很忙。 是的,当工人完成它的工作时,建议(从我这边)再次启动它来执行一些其他任务。 是的,只要完成,完成的事件将被解雇。 webclient事件在backgroundworker事件中,webclient完成事件将是第一个。您必须先调试代码才能看到代码的顺序流程。

相关问题