【问题标题】:Background worker will not run "RunWorkerCompleted" after e.cancel = true;e.cancel = true 后后台工作人员不会运行“RunWorkerCompleted”;
【发布时间】:2012-12-17 12:36:16
【问题描述】:

这是工作代码。我什至已经通过了这一点。在 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;

【问题讨论】:

  • 修正了抱歉,这个网站总是让我感到困惑,我从来没有意识到我没有给出最好的答案。

标签: c# multithreading backgroundworker deadlock


【解决方案1】:

你设置了吗

BackgroundWorker.WorkerSupportsCancellation = true

?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多