2

这是dowork代码。我什至已经通过了这一点。在 e.cancel = true 之后,DoWork 再次运行,进入 while 循环,再次将 e.Cancel 设置为 true,然后退出函数,不再运行 Completed 函数。

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        //While the song is not over
          while (!worker.CancellationPending )
          {

              if (progressBar1.Value == progressBar1.Maximum)
              {

                  e.Cancel = true;
                  return;
              }
              else
              {
                  //Keep ticking the progress bar one second
                  worker.ReportProgress(0);
                  Thread.Sleep(1000);
              }
          }
          e.Cancel = true;
          return;



    }

这是取消工人的代码。WaitOne() 将阻塞,直到它从 RunWorkerCompleted 收到信号。

if (this.backgroundWorker2.IsBusy)
        {
            this.backgroundWorker2.CancelAsync();

            _resetEvent.WaitOne();

        }

编辑:请注意,我已经完成了这个 VV

backgroundWorker2.RunWorkerCompleted += backgroundWorker2_RunWorkerCompleted;
backgroundWorker2.WorkerSupportsCancellation = true;
4

1 回答 1

1

你设置了吗

BackgroundWorker.WorkerSupportsCancellation = true

?

于 2012-12-17T04:34:31.020 回答