我如何才能暂停/继续后台工作人员

本文关键字:继续 后台 工作人员 暂停 | 更新日期: 2024-10-22 05:36:08

我有一个按钮点击事件要继续:

 private void button3_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                button2.Enabled = true;
                button3.Enabled = false;
            }
            else
            {
                backgroundWorker1.RunWorkerAsync();
                button2.Enabled = true;
                button3.Enabled = false;
            }
        }

以及暂停的按钮点击事件:

private void button2_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                backgroundWorker1.CancelAsync();
                button3.Enabled = true;
                soundPlay = false;
                stop_alarm = true;
            }
        }

问题是按钮3点击事件继续代码有时后台很忙,所以我可以启用false/true按钮,但后台工作人员仍在工作。我想暂停DoWork事件并继续。

这是我的DoWork活动:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            while (true)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    if (tempCpuValue >= (float?)numericUpDown1.Value || tempGpuValue >= (float?)numericUpDown1.Value)
                    {
                        soundPlay = true;
                        blinking_label();
                    }
                    else
                    {
                        soundPlay = false;
                    }
                    cpuView();
                    gpuView();
                    Thread.Sleep(1000);
                }
            }
        }

我如何才能暂停/继续后台工作人员

您可以使用Thread而不是BackgroundWorker来执行此操作吗?

使用Thread,在其工作子例程中,您可以将线程置于捕获ThreadInterruptedException的try/catch块内的无限休眠状态。因此,当它循环处理它正在处理的任何事情时,您可以查看布尔标志的值来知道是否休眠,然后调用Thread.Sleep(Timeout.Infinite)。当您捕获ThreadInterruptedException时,您将标志设置为false并继续执行。

要使线程从UI恢复,您需要将"pause"标志设置为false,并在线程上调用workerThread.Interrupt()(假设您称之为workerThread)。

想法来源于此处

您可能需要等到取消完成后才能启用/禁用按钮。

private AutoResetEvent _resetEvent = new AutoResetEvent(false);
private void button2_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                backgroundWorker1.CancelAsync();
                //Wait statement goes here.
                this.Cursor=Cursors.AppStarting;
                _resetEvent.WaitOne(); // will block until _resetEvent.Set() call made
                button3.Enabled = true;
                soundPlay = false;
                stop_alarm = true;
            }
        }

请看这个问题:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            while (true)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    if (tempCpuValue >= (float?)numericUpDown1.Value || tempGpuValue >= (float?)numericUpDown1.Value)
                    {
                        soundPlay = true;
                        blinking_label();
                    }
                    else
                    {
                        soundPlay = false;
                    }
                    cpuView();
                    gpuView();
                    Thread.Sleep(1000);
                }
            }
              _resetEvent.Set(); // signal that worker is done
        }