Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Timers Timer not firing event

Tags:

c#

wpf

timer

I have to start timer on the event when eyes are closed for a certain duration.If timer is elapsed Screen turns Off.If eye open before timer is elapsed timer is stopped and screen turns On.

ComputationOfTimer(); monitors whether eyes are open/closed. This is working fine as I am getting right feedback in console.

            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() starts the timer

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

Logic of Screen Off and Sleep

            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(); to stop the timer if Eyes are opened earlier

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

        }

Timer is not firing even after time is elapsed.I tried DispatcherTimer before but that is to update WPF UI and I have different objective.

Declaration part:

System.Timers.Timer screenOffTimer = new System.Timers.Timer();
System.Timers.Timer gotoSleepTimer = new System.Timers.Timer();
like image 815
Nav S Avatar asked Dec 18 '25 20:12

Nav S


1 Answers

try

EventTabTimer.Elapsed += new ElapsedEventHandler(gotoSleepTimer_Tick_SleepOff);
like image 174
Balwinder SIngh Avatar answered Dec 21 '25 10:12

Balwinder SIngh