How to set timer to execute at specific time in c#

后端 未结 4 2129
南旧
南旧 2020-12-09 18:22

I have a requirement where i need to execute timer at 00:01:00 A.M every day...But i am not getting how to achieve this ..If i am taking Systems time,it can be in different

4条回答
  •  有刺的猬
    2020-12-09 18:44

    You could always calculate it:

    static void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        // Do stuff
    
        start_timer();
    }
    
    private static void start_timer()
    {
        timer.Interval = CalculateInterval();
        timer.Start();
    }
    
    private static double CalculateInterval()
    {
        // 1 AM the next day
        return (DateTime.Now.AddDays(1).Date.AddHours(1) - DateTime.Now).TotalMilliseconds;
    }
    

提交回复
热议问题