3

当眼睛闭上一段时间后,我必须在事件上启动计时器。如果计时器到时,屏幕会关闭。如果在计时器过去之前眼睛睁开,计时器会停止并且屏幕会打开。

计算计时器();监控眼睛是否睁开/闭上。这很好,因为我在控制台中得到了正确的反馈。

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

        }

计时器停止();如果较早睁开眼睛,则停止计时器

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

2 回答 2

1

尝试

EventTabTimer.Elapsed += new ElapsedEventHandler(gotoSleepTimer_Tick_SleepOff);
于 2013-08-28T13:12:58.013 回答
0

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

谢谢

于 2013-08-28T12:48:10.513 回答