Run once a day in C#

后端 未结 10 2100
不思量自难忘°
不思量自难忘° 2020-12-09 05:00

Is there any clever method out there to make my executeEveryDayMethod() execute once a day, without having to involve the Windows TaskScheduler?

10条回答
  •  -上瘾入骨i
    2020-12-09 05:24

    To run the job once daily between 7 and 8pm, i set up a timer with interval = 3600000 ms and then just execute the following code for timer tick.

    private void timer1_Tick(object sender, EventArgs e)
    {
        //ensure that it is running between 7-8pm daily.
        if (DateTime.Now.Hour == 19)
        { 
            RunJob(); 
        }
     }
    

    An hour window is fine for me. Extra granularity on time will require a smaller interval on the timer (60000 for a minute) and including minutes on the if.

    eg

    {
        //ensure that it is running at 7:30pm daily.
        if (DateTime.Now.Hour == 19 && DateTime.Now.Minute == 30)
        { 
            RunJob(); 
        }
     }
    

提交回复
热议问题