c# - Background Worker Check For When It's Midnight? -
I want to create a background worker for a WinForm that triggers code until it rolls in midnight.
I have an idea to do this, but I'm pretty sure that this is not the best way to do it.
while (1 == 1) {// if Datetime.Now == Midnight, execute code // sleep (1 second)}
Use a System.Timers.Timer
and start the application simply by DateTime.Now
and DateTime.Today.AddDays (0)
. Then set the gap for that amount.
I actually did something like this:
Public stable class DayChangedNotifier {Private stationary timer timer; Static DayChangedNotifier () {Timer = New Timer (GetSleepTime ()); Timer Elapsed + = (O, E) = & gt; {Started chained (datetime.now.DayOfWeek); Timer.Interval = this.GetSleepTime (); }; Timer.start (); SystemEvents.TimeChanged + = New Event Handler (SystemEvents_TimeChanged); } Private Static Zero SystemEvents_TimeChanged (Object Sender, EventArgs e) {timer.Interval = GetSleepTime (); } Private Static Double GetSleepTime () {var midnightTonight = DateTime.Today.AddDays (1); Var difference INMilliseconds = (midnight night - date time.Now). Total EmilisCand; Withdrawal interim cylinders; } Private Static Zero OnDayChanged (DayOfWeek Day) {var Handler = DayChanged; If (handler! = Null) {handler (empty, new day changing events event (days)); }} Public Stable Event EventHandler & lt; DayChangedEventArgs & gt; DayChanged; }
and:
public square daydreams: EventsErgues {Public De Chase Event Day (Day of Week) {this.DayOfWeek = day; } Public Deaf Wake Deaf Wake {Receive; Private set; }}
Usage: DangerousNotified. Dilated + = .....
Comments
Post a Comment