Use System.Timers.Timer and when you start the application, simply calculate the difference between DateTime.Now and DateTime.Today.AddDays(0) . Then set the interval for this amount.
Recently, I have really been doing something like this:
public static class DayChangedNotifier { private static Timer timer; static DayChangedNotifier() { timer = new Timer(GetSleepTime()); timer.Elapsed += (o, e) => { OnDayChanged(DateTime.Now.DayOfWeek); timer.Interval = this.GetSleepTime(); }; timer.Start(); SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged); } private static void SystemEvents_TimeChanged(object sender, EventArgs e) { timer.Interval = GetSleepTime(); } private static double GetSleepTime() { var midnightTonight = DateTime.Today.AddDays(1); var differenceInMilliseconds = (midnightTonight - DateTime.Now).TotalMilliseconds; return differenceInMilliseconds; } private static void OnDayChanged(DayOfWeek day) { var handler = DayChanged; if (handler != null) { handler(null, new DayChangedEventArgs(day)); } } public static event EventHandler<DayChangedEventArgs> DayChanged; }
and
public class DayChangedEventArgs : EventArgs { public DayChangedEventArgs(DayOfWeek day) { this.DayOfWeek = day; } public DayOfWeek DayOfWeek { get; private set; } }
Usage: DayChangedNotified.DayChanged += ....
Bfree
source share