2012-08-07 66 views
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点击事件的继续代码有时背景是很忙,所以我可以启用虚假/真实的按钮,但背景工作正在继续工作。 我想暂停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); 
       } 
      } 
     } 

回答

2

你可以用Thread而不是BackgroundWorker做到这一点?

使用线程,在其工作子例程中,您可以将线程置于try/catch块内的无限睡眠中,捕获ThreadInterruptedException。所以,当它循环处理任何工作时,您可以查看布尔标志的值来知道是否要睡眠,然后致电Thread.Sleep(Timeout.Infinite)。当你赶上ThreadInterruptedException你设置标志为假,并继续执行。

要使线程从您的用户界面恢复,您需要将“暂停”标志设置为false,并在您的线程上调用workerThread.Interrupt()(假设您称之为workerThread,那就是)。

理念从here

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

     } 

Please see this question:

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 
     }