【问题标题】:Download file with backgroundWorker使用 backgroundWorker 下载文件
【发布时间】:2011-09-07 08:33:31
【问题描述】:

我已经创建了下载文件

private void btnTestDownload_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
} 

然后工作!但是

private void btnTestDownload_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}

backgroundWorker 不会停止!

【问题讨论】:

  • 您的 worker 代码在哪里,它是否处理取消?

标签: c# winforms visual-studio-2010


【解决方案1】:

CancelAsync 不会导致工作人员停止。您必须手动执行此操作。在 worker 方法中,你必须定期检查它的 CancellationPending 属性,看看它是否应该取消。

所以基本上DoWork 方法的主体应该是这样的:

foreach( var something in somethingelse )
{
  if (worker.CancellationPending == true) {
    e.Cancel = true;
    return;
  }
  //do your work here
}

如果你在你的worker方法中只是调用了其他一些本身需要很长时间才能完成的方法,并且你没有可能自己定期检查CancellationPending变量,那么在命令中停止worker并不容易无需强行破坏线程。

【讨论】:

    【解决方案2】:

    见MSDN:

    当您调用 CancelAsync 时,您的工作方法有机会 停止执行并退出。工作人员代码应定期检查 CancellationPending 属性以查看它是否已设置为 true。

    【讨论】:

      【解决方案3】:

      听起来您对后台工作人员的目的完全有错误的想法。如果您想异步下载单个文件并能够取消它,所有这些功能都内置在 WebClient 类中。

      后台工作人员用于长时间运行的任务,这些任务总体上是处理器密集型的。例如,如果您正在下载的文件是一个大文本文件,并且您需要解析文本文件的每一行,您可以使用后台工作程序,例如

      下载文件

          WebClient Client = new WebClient();
      
          public void TestStart()
          {
              //Handle the event for download complete
              Client.DownloadDataCompleted += Client_DownloadDataCompleted;
              //Start downloading file
              Client.DownloadDataAsync(new Uri("http://mywebsite.co.uk/myfile.txt"));
          }
      
          void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
          {
              //Remove handler as no longer needed
              Client.DownloadDataCompleted -= Client_DownloadDataCompleted;
              //Get the data of the file
              byte[] Data = e.Result;
          }
          public void TestCancel()
          {
              Client.CancelAsync();
              Client.DownloadDataCompleted -= Client_DownloadDataCompleted;
          }
      

      处理文件

          BackgroundWorker worker = new BackgroundWorker() { WorkerSupportsCancellation = true };
      
          //Take a stream reader (representation of a text file) and process it asyncronously
          public void ProcessFile(StreamReader Reader)
          {
              worker.DoWork += worker_DoWork;
              worker.RunWorkerAsync(Reader);
          }
          public void CancelProcessFile()
          {
              worker.CancelAsync();
          }
      
          void worker_DoWork(object sender, DoWorkEventArgs e)
          {
              //Get the reader passed as an argument
              StreamReader Reader = e.Argument as StreamReader;
              if (Reader != null)
              {
                  //while not at the end of the file and cancellation not pending
                  while (Reader.Peek() != -1 && !((BackgroundWorker)sender).CancellationPending)
                  {
                      //Read the next line
                      var Line = Reader.ReadLine();
                      //TODO: Process Line
                  }
              }
          }
      

      【讨论】:

      • WebClient 还具有 DownloadFileAsync 和 DownloadStringAsync ,具体取决于您使用它的目的。事件处理程序将有所不同,但您使用相同的取消方法。
      猜你喜欢
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2015-04-17
      • 2010-12-20
      相关资源
      最近更新 更多