Trigger event using timer on a specific day and time
12,473
It isn't clear why you just wouldn't set the timer's Interval to the target date/time. There's a limit on the number of milliseconds, you can time up to 2^31 milliseconds, 27 days. You'll be good as long as you can stay in that range.
private static void SetTimer(Timer timer, DateTime due) {
var ts = due - DateTime.Now;
timer.Interval = ts.TotalMilliseconds;
timer.AutoReset = false;
timer.Start();
}
Author by
fireBand
Updated on June 04, 2022Comments
-
fireBand about 2 years
I am using System.Timer to trigger an event. Currently I trigger it every 1 hour and check if it matches the configured value (day,time).
But it is possible to trigger this at a specific time? like suppose on Sunday at 12Am.
Windows Task Scheduler would be more appropriate but its not an option.
Thanks in advance
-
fireBand over 13 yearswould it be possible to set timer on DayOfWeek rather than the Date? Like Sunday at 12 Am or Tuesday at 1 Am? Thanks
-
user1703401 over 13 yearsSure, just use AddDays() to add the number of days between DayOfWeek and Now.DayOfWeek, casted to int. Add 7 if the value is negative.