Introduction
I'm sure that a large percentage of developers have wished for a way to detect when it is midnight on a machine and execute code which updates something simple and non complex (i.e. display dates, instead of "Today" you would want it to show "Yesterday" and so forth).
I pondered over this for some time until I had inspiration one late night. Let's use a Timer to do it, but with some intelligence.
Background
While working on a larger project, I needed a way to ensure that an event would fire which would execute code to update the displayed days (much how Outlook displays emails by date) at midnight.
I thought of various methods to accomplish this. Check the datetime every second.. no, Check the time ever x hours... no... Just loop until midnight is reached... no!
Each way I thought of included heavy looping or polling in order to check the time to know when it turns midnight. So a new approach was needed.
I realised that I could use a system Timer to achieve what I wanted without reinventing the wheel.
Using the Code
I've designed the code in a simplistic way in order for you to use the code more effectively.
I have contained the logic in a single class, which has 3 public
method, Start()
, Restart()
and Stop()
.
When the class is first executed via the Start()
method, the code obtains a datetime of the current time, then gets a datetime object of midnight and subtracts the 2 times in order to give a time until it is midnight.
With this time, it sets the timer interval and starts the timer. Now when the interval is reached and the timer fires its event, I reset the timer using the same process. This will make the interval for 24. When this timer expires, it is then reset and repeated indefinitely.
To use the class in your code, simply add these lines.
Insert the following into the method you wish to call the Timer from:
MidnightTimer m_MidnightTimer = new MidnightTimer();
m_MidnightTimer.TimeReached += new TimeReachedEventHandler(this.m_MidnightTimer_TimeReached);
m_MidnightTimer.Start();
Add the Event Handler method:
private void m_MidnightTimer_TimeReached(DateTime Time)
{
}
That's it! The timer will now execute the method m_MidnightTimer_TimeReached
at Midnight.
Behind the scenes
Let's look at the main piece behind this code:
public void Start()
{
TimeSpan ts = this.GetMidnight(s_MinutesAfterMidnight).Subtract(DateTime.Now);
TimeSpan tsMidnight = new TimeSpan(ts.Hours, ts.Minutes, ts.Seconds);
s_timer = new Timer(tsMidnight.TotalMilliseconds);
s_timer.Elapsed += new ElapsedEventHandler(this.timer_Elapsed);
Microsoft.Win32.SystemEvents.TimeChanged += new EventHandler(this.WindowsTimeChangeHandler);
s_timer.Start();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
s_timer.Stop();
OnTimeReached();
this.Start();
}
Windows Time Change
Thanks to Nicole1982 and BruceN for this (see the comments).
When the time of Windows is changed, the timer picks it up and automatically resets for you.
This is done via the Microsoft.Win32.SystemEvents.TimeChanged
event in the Start()
method which invokes the WindowsTimeChangeHandler
WindowsTimeChangeHandler method, which simply calls the Restart()
method.
private void WindowsTimeChangeHandler(object sender, EventArgs e)
{
this.Restart();
}
Note: This is a known issue with the Microsoft.Win32.SystemEvents.TimeChanged
firing twice when the system is changed as outlined at https://connect.microsoft.com/VisualStudio/feedback/details/776003/systemevent-timechanged-is-fired-twice
Inside GetMidnight
As you can guess, the GetMidnight, simply returns midnight. Additionally the method allows for the supply of how many minutes after midnight you have
private DateTime GetMidnight(int MinutesAfterMidnight)
{
DateTime Tomorrow = DateTime.Now.AddDays(1);
return new DateTime(Tomorrow.Year, Tomorrow.Month, Tomorrow.Day, 0, MinutesAfterMidnight, 0);
}
What the Start()
method actually does is obtain the current time and midnight of the next day (DateTime.Now.Day+1
) and subtracts them to give us how many hours, minutes, seconds until midnight occurs. This is the value we use to set the initial timer to fire at midnight.
Now after a few hours pass by and it hits midnight, the timer fires and recalls Start()
(as well as stops the original timer, etc.) and repeats the process of getting the time now, and the time of the next midnight (in this case, 24 hours) and resets the timer.
Minutes after Midnight
The timer also supports supplying how many minutes after midnights you would like it to fire at.
To enable this, simply supply how many minutes via an integer parameter in the midnight timer constructor overload.
MidnightTimer m_MidnightTimer = new MidnightTimer(10);
m_MidnightTimer.TimeReached += new TimeReachedEventHandler(this.m_MidnightTimer_TimeReached);
m_MidnightTimer.Start();
Version 2.0 Note
Recently I was involved in a project in which I needed this code again and having read the comments I took them on board and applied them to the code. Thanks to everyone who left constructive feedback, bug fixes and pointed me in the right direction to enhance this small but simple solution.
8 years is a long time! o_0 Happy coding!
Feedback
Feedback is most welcome! In fact I insist on it!
If you have any improvements or ideas on how to improve this code, please speak up and voice your opinion! It's a great way to learn!
History
- 1.0.0.0 - 30/03/2007 - Initial version
- 2.0.0.0 31/03/2015 - Bug fixes, Windows Timer Hook, More comments, updated article to reflect code changes