1

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

 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;
            }

        }

问题在于button3单击事件,有时背景很忙,所以我可以启用false/true按钮,但backgroundworker继续工作。我想暂停 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);
                }
            }
        }
4

2 回答 2

2

你可以用 aThread而不是a 来做到这一点BackgroundWorker

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

要使线程从您的 UI 恢复,您可以将“暂停”标志设置为 false 并调用workerThread.Interrupt()您的线程(假设您将其称为 workerThread)。

想法来源于这里

于 2012-08-07T04:55:10.100 回答
0

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

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
        }
于 2012-08-07T05:25:25.737 回答