2012-08-07 62 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 
     }