How to set a timer to execute at a specific time in C #

I have a requirement when I need to run a timer at 00:01:00 AM every day ... But I do not understand how to achieve this. If I take the System time, it may be in a different format., Here is my timer code.

static System.Timers.Timer timer; timer = new System.Timers.Timer(); timer.Interval = 1000 * 60 * 60 * 24;//set interval of one day timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); start_timer(); static void timer_Elapsed(object sender, ElapsedEventArgs e) { // Add timer code here } private static void start_timer() { timer.Start(); } 
+7
multithreading c # timer
source share
4 answers

What you need to do is write your program that will do everything you need, and then use the built-in task scheduler of your OS to disable it. That would be the most reliable. Windows Task Scheduler, for example, can launch your application before a user logs in, restart the application if necessary, register errors and send notifications, etc.

Otherwise, you will need to run the application 24 hours a day and 7 days a week, as well as conduct a survey over time at regular intervals.

For example, you can change the interval every minute:

 timer.Interval = 1000 * 60; 

And inside your Elapsed event, check the current time:

 static void timer_Elapsed(object sender, ElapsedEventArgs e) { if (DateTime.Now.Hour == 1 && DateTime.Now.Minute == 0) { // do whatever } } 

But it is really unreliable. Application may crash. And working with DateTime can be tricky.

+8
source share

If you want to start the timer at exactly 00:01:00, do some processing time and then restart the timer that you need to calculate the difference between Now and the next time interval 00:01:00 in the morning, for example.

 static Timer timer; static void Main(string[] args) { setup_Timer(); } static void setup_Timer() { DateTime nowTime = DateTime.Now; DateTime oneAmTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 0, 1, 0, 0); if (nowTime > oneAmTime) oneAmTime = oneAmTime.AddDays(1); double tickTime = (oneAmTime - nowTime).TotalMilliseconds; timer = new Timer(tickTime); timer.Elapsed += timer_Elapsed; timer.Start(); } static void timer_Elapsed(object sender, ElapsedEventArgs e) { timer.Stop(); //process code.. setup_Timer(); } 
+23
source share

You can 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; } 
0
source share

Here is an implementation of a timer that takes an interval (like any other timer) and fires exactly after this interval has expired, even if the machine goes into sleep mode between them.

 public delegate void TimerCallbackDelegate(object sender, ElapsedEventArgs e); public class TimerAbsolute : System.Timers.Timer { private DateTime m_dueTime; private TimerCallbackDelegate callback; public TimerAbsolute(TimerCallbackDelegate cb) : base() { if (cb == null) { throw new Exception("Call back is NULL"); } callback = cb; this.Elapsed += this.ElapsedAction; this.AutoReset = true; } protected new void Dispose() { this.Elapsed -= this.ElapsedAction; base.Dispose(); } public double TimeLeft { get { return (this.m_dueTime - DateTime.Now).TotalMilliseconds; } } public int TimeLeftSeconds { get { return (int)(this.m_dueTime - DateTime.Now).TotalSeconds; } } public void Start(double interval) { if (interval < 10) { throw new Exception($"Interval ({interval}) is too small"); } DateTime dueTime = DateTime.Now.AddMilliseconds(interval); if (dueTime <= DateTime.Now) { throw new Exception($"Due time ({dueTime}) should be in future. Interval ({interval})"); } this.m_dueTime = dueTime; // Timer tick is 1 second this.Interval = 1 * 1000; base.Start(); } private void ElapsedAction(object sender, System.Timers.ElapsedEventArgs e) { if (DateTime.Now >= m_dueTime) { // This means Timer expired callback(sender, e); base.Stop(); } } } 
0
source share

All Articles