I’d like to get notified when Windows system date/time is changed in my C# app. I’ve found the SystemEvents.TimeChanged event, for which the MSDN documentation says:
Occurs when the user changes the time on the system clock.
Does this event also get fired if the clock is changed by NTP? Or is there another event I can subscribe to?
Implementation details:
I want a timer.Elapsed event to fire at a specific date/time. My code calculates the interval required to use for the timer. If there’s a system time change, this calculation needs to be re-evaluated to work out the new interval and update the timer.
Summary of answers
Thanks for all the comments. I’ve summarised them below for future reference:
- SystemEvents.TimeChanged fires on user change and when the system calls for a resync with the NTP server
- If running in a windows service, the message pump needs to be processed to get the TimeChanged event. This can be achieved by running a dispatcher.
- The NTP server doesn’t apply a discrete system date change. NTP clock adjustment are applied gradually, taking into account the calculated clock drift and the offset between the local clock and other NTP servers
- For my implementation, it would be better to evaluate the timer interval periodically to take into account any system time changes, rather than setting the interval once and leaving it to fire.
Yes,
TimeChangedis raised when you configure Windows to use an NTP server to keep your time up to date as well as when someone manually changes the time.One caveat here is that you must have a UI. (or start a hidden window that to get a message pump). I.e. you can’t get a TimeChanged event in a Windows service.