2013-08-28 86 views
3

當眼睛閉合一段時間時,我必須啓動事件計時器。如果計時器已過,屏幕將關閉。如果在計時器過去之前睜大眼睛計時器停止並且屏幕打開。System.Timers計時器未觸發事件

ComputationOfTimer();監視眼睛是否打開/關閉。這是工作正常,因爲我在控制檯中獲得正確的反饋。

  private void ComputationOfTimer() 
     { 
      if (blink[0] == 100)  //If eye Closed detected 
      { 
       ctrlTimerStop = 3; 
       ctrlTimerStart = ctrlTimerStart - 1; 
       System.Console.Write("\n\t Eyes Closed");      
       timerStarting(); 
      } 
      else      //If eyes are open before timer is elapsed 
      //we have to stop timer 
      { 
       ctrlTimerStart = 5; 
       ctrlTimerStop -= 1; 
       //System.Console.Write("\n\t\t\t\t\t Opened"); 
       timerStopping(); 
      } 
     } 

timerStarting()啓動定時器屏幕關閉的

  public void timerStarting() 
     { 
      if (ctrlTimerStart == 0) 
      { 

       screenOffTimer.Interval = 3000; 
       screenOffTimer.Elapsed += screenOffTimer_Tick_ScreenOff; 
       screenOffTimer.AutoReset=false; 

       if (!screenOffTimer.Enabled) //Starts timer only once 
       { 
        screenOffTimer.Enabled = true; 
        System.Console.Write("Timer Chaloo Hai"); 
       } 
      } 
     } 

邏輯和睡眠

  void screenOffTimer_Tick_ScreenOff(object sender, EventArgs e) 
     { 
      System.Console.Write("Eyes Closed For long time bro!"); 
      Beep(440, 1000); // Concert A, for 1 second 
      SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF); 

    //as eyes are still closed send pc to Sleep start one more timer 

      gotoSleepTimer.Interval = 10000; 
      gotoSleepTimer.Elapsed += gotoSleepTimer_Tick_SleepOff; 
      gotoSleepTimer.AutoReset = false; 

      if (!gotoSleepTimer.Enabled) 
      { 
       gotoSleepTimer.Start(); 
      }     

     } 

     void gotoSleepTimer_Tick_SleepOff(object sender, EventArgs e) 
     { 
      System.Console.Write("So rahe hain bhai ab"); 
      Beep(440, 2000); // Concert A, for 1 second 
      System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend, false, false); 

     } 

timerStopping();如果眼睛被打開更早

  public void timerStopping()  //To stop timer when Eyes Open 
     { 
      if (ctrlTimerStop == 0) 
      { 
       //to do timer stop logic 
       if (screenOffTimer.Enabled) 
       { 
        screenOffTimer.Stop(); 
        System.Console.Write("Timer Band Ho Gaya"); 
       } 
       //System.Windows.MessageBox.Show("Timer Stopped"); 
       SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON); 

       if (gotoSleepTimer.Enabled) 
       { 
        gotoSleepTimer.Stop(); 
       } 
      } 

     } 

定時器不點火即使時間elapsed.I之前試過DispatcherTimer但這是更新WPF UI和我有不同的目標,以停止計時器。

聲明部分:

System.Timers.Timer screenOffTimer = new System.Timers.Timer(); 
System.Timers.Timer gotoSleepTimer = new System.Timers.Timer(); 
+0

定時器可以很好地運行,但由於未處理的異常被它會被忽略,你可以在防止它的方法某處有一個例外從成功完成。嘗試在定時器回調中添加try/catch。 – jgauffin

+0

好的,我會試試,謝謝。 –

回答

1

嘗試

EventTabTimer.Elapsed += new ElapsedEventHandler(gotoSleepTimer_Tick_SleepOff); 
+0

感謝它現在的工作屏幕關閉,但不是要睡覺屏幕不斷閃爍。現在我必須改進該邏輯。再次感謝您的快速解決方案。 –

0

我看不出在連接有問題的代碼screenOffTimer.Start()。可能這就是問題

感謝

+0

** screenOffTimer.Enabled = true; **與** screenOffTimer.Start()相同**不是。 –