1

我想在完成后再次运行我的后台工作人员..就像

backgroundWorker1.do 工作,然后后台工作人员完成,然后再次运行后台 worker1.do 工作......怎么做......请注意,我必须一次又一次地运行许多后台工作......谢谢

4

3 回答 3

1

您可以RunWorkerAsync()RunWorkerCompleted事件处理程序中添加调用

    bw.RunWorkerCompleted += bw_RunWorkerCompleted;

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        ((BackgroundWorker)sender).RunWorkerAsync();
    }
于 2012-08-28T12:45:13.603 回答
0

如果您使用的是 .NET 4.0 或 .NET 4.5,则可以使用Tasks而不是 BackgroundWorker:

// Here your long running operation
private int LongRunningOperation()
{
   Thread.Sleep(1000);
   return 42;
}

// This operation will be called for processing tasks results
private void ProcessTaskResults(Task t)
{
   // We'll call this method in UI thread, so Invoke/BeginInvoke
   // is not required
   this.textBox.Text = t.Result;

}

// Starting long running operation
private void StartAsyncOperation()
{
   // Starting long running operation using Task.Factory
   // instead of background worker.
   var task = Task.Factory.StartNew(LongRunningOperation);   

   // Subscribing to tasks continuation that calls
   // when our long running operation finished
   task.ContinueWith(t =>
   {
      ProcessTaskResults(t);
      StartOperation();
   // Marking to execute this continuation in the UI thread!
   }, TaskScheduler.FromSynchronizationContext);
}

// somewhere inside you form's code, like btn_Click:
StartAsyncOperation();

在处理长时间运行的操作时,基于任务的异步是一种更好的方法。

于 2012-08-28T13:12:35.777 回答
0

也许您可以创建一个具有相同属性的新 Backgroundworker,或者您只需在完成时调用 backgroundworker1.doWork()。

于 2012-08-28T12:43:47.490 回答