I have a timer class which uses the date / time of the local machine to work out how long things have been running and to set timers that expire after a certain period.
I’ve discovered that if a user changes the system time, this causes havoc with my application as the timers suddenly think they’ve expired or the time recorded by them suddenly jumps by an hour (say).
These timeres are currently used through a multi-threaded application.
The timers / timeout timers run for anything from a few seconds to a few days.
How can I avoid these timing inconsistencies?
You can use the SystemEvents.TimeChanged event to get notified of this.
You also need to worry a bit about the machine’s time zone or daylight savings rules changing. .NET ensures that this doesn’t cause major disruption on timers by locking in the zone the first time a datetime is retrieved. In other words, it ensures timers are expire at their programmed interval rather than their absolute time. If that’s an issue (test it) then you’ll need to explicitly clear that state by calling CultureInfo.ClearCachedData() (for TimeZone and DateTime) as well as TimeZoneInfo.ClearCachedData() (for TimeZoneInfo and DateTimeOffset).
Lots of other things that can go wrong when the machine’s clock is adjusted. The file system timestamps for example. The Linux leap second bug was an interesting recent event. Having a solid time source is rather important to keep machines humming, easily obtained from a time server.