System.Timers计时器未触发事件

时间:2013-08-28 12:44:06

标签: c# wpf timer

当眼睛关闭一段时间后,我必须在事件上启动计时器。如果计时器已经过,屏幕将关闭。如果在计时器结束前睁开眼睛,计时器将停止并且屏幕会亮起。

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

        }

计时器即使在经过一段时间后也没有激活。我之前尝试过DispatcherTimer,但那是为了更新WPF UI,我有不同的目标。

声明部分:

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

2 个答案:

答案 0 :(得分:1)

EventTabTimer.Elapsed += new ElapsedEventHandler(gotoSleepTimer_Tick_SleepOff);

答案 1 :(得分:0)

我在相关代码中看不到screenOffTimer.Start()。可能就是问题

由于