How to determine if the system time has changed? (from .net)

I use DateTime.UtcNow to measure time in my software and should be able to tell if "action a" occurs less than 10 seconds after "action b"

However, what if the system time changes? So, how can I determine if the system time has changed?

I do not want to use a stopwatch, since we need to run on servers with more than one processor, see http://kristofverbiest.blogspot.com/2008/10/beware-of-stopwatch.html

I also need to deal with pausing and restarting virtual machines, so the "number of ticks" is unlikely to be useful to me.

+7
windows time
source share
4 answers

You can use the SystemEvents.TimeChanged event to detect a time change. However, this will only work if the messagepump program is running - this is normal for Windows client applications, but not for most server applications (such as services, web applications, etc.).

Otherwise, you can use Environment.TickCount - it does not have excellent resolution, but if your "10" seconds can be "almost 10 seconds", you are probably fine. Note that TickCount will roll over when the machine has been running long enough, so you will have to handle this, possibly with a DateTime.UtcNow security check anyway.

I would use a combination of both - but note that there is absolutely no exact way, besides checking some external timer that is known exactly (which will have the primary effect) that will work in the case of VM anyway ... make sure you define that is "reliable enough" before you spend more time than the problem.

+8
source share

I would suggest using StopWatch . If the problem described in the Beware of QueryPerformanceCounter exists, you say a few milliseconds difference. If you are concerned about negative time (something that I have never seen, and I often use StopWatch few short periods of time), then treat anything between -1 millisecond and -1000 milliseconds as zero.

Using DateTime.Now or DateTime.UtcNow for any type of time will cause you problems if someone changes the date / time on the machine. You must use something that is completely internal to the computer and cannot be changed. StopWatch is the most reliable I've found.

+4
source share

You can use the TimeChanged event for the SystemEvents class to detect changes in the system clock at runtime:

http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.timechanged.aspx

It depends on your application having a message loop (I assume that the WM_TIMECHANGED message needs to be processed), so you will need a hidden form if it is a headless application, for example. Windows

As for detecting changes in the system clock, while your application is suspended due to the fact that it is located on a suspended virtual machine, all the bets are valid for a really reliable solution. You could periodically record the last time you checked, and just make sure that the time has not passed. In any case, it will probably take more than ten seconds to pause and resume the virtual machine.

+1
source share

I would compare the difference in system time with a monotic clock between two time intervals. If the difference is greater than some tolerance than someone changed the date and time on your computer.

0
source share

All Articles