How to use the .NET Timer class to trigger an event at a specific time?

前端 未结 9 1685

I would like to have an event triggered in my app which runs continuously during the day at a certain time, say at 4:00pm. I thought about running the timer every second and

9条回答
  •  天命终不由人
    2020-12-01 00:28

    I did this way to fire 7am every morning

    bool _ran = false; //initial setting at start up
        private void timer_Tick(object sender, EventArgs e)
        {
    
            if (DateTime.Now.Hour == 7 && _ran==false)
            {
                _ran = true;
                Do_Something();               
    
            }
    
            if(DateTime.Now.Hour != 7 && _ran == true)
            {
                _ran = false;
            }
    
        }
    

提交回复
热议问题