Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which timers are dependent on system time?

I haven't tested this yet. I am hoping someone already knows the answer, so I don't have to write a test application, otherwise I will. :)

Usually when I want to compare time, I just store DateTime.Now and compare it at a later time. I believe this gives wrong results when the user changes the user's system time somewhere in between.

It made me wonder whether timers behave the same way. Which .NET timers are dependent on the set system time? Consider starting a timer which needs to elapse in one hour. Will it trigger when I set system time one hour forward?

I never really cared about this possible behavior, but it might be important in some scenarios. Which timers are safe against this scenario, and which ones most definitely aren't?

like image 985
Steven Jeuris Avatar asked Feb 26 '11 16:02

Steven Jeuris


People also ask

What is the use of system timer?

The System. Timers. Timer class has the same resolution as the system clock. This means that the Elapsed event will fire at an interval defined by the resolution of the system clock if the Interval property is less than the resolution of the system clock.

Does system timers timer run in a separate thread?

Yes, they run in a different thread.

What is System time format?

System time is the current date and time of day. The system keeps time so that your applications have ready access to accurate time. The system bases system time on coordinated universal time (UTC). UTC-based time is loosely defined as the current date and time of day in Greenwich, England.


2 Answers

I was trying to solve similar problem. I turn out to use System.Diagnostic.StopWatch to replace all DateTime.Now. StopWatch will use the high frequency clock if present. So, it's more accurate and independent of the system clock change. However, if high frequency clock is not present, it will fall back to use system clock again.

According to my testing, all my machines have high frequency clock, including the machines in VM.

About the Timer, as far as I remember, it isn't dependent on the system clock. However, you don't really want to use Timer to track the time because the Timer callback events may be deferred by some other events.

like image 85
Harvey Kwok Avatar answered Oct 07 '22 07:10

Harvey Kwok


I'll just quote Jim Mischel comments, as it's the most relevant answer to my question.

None of the timers depend on the system time. That is, the user changing the clock will not affect System.Windows.Forms.Timer, System.Timers.Timer, or System.Threading.Timer. Nor will it affect Stopwatch or Environment.TickCount. Also, there's no "overhead" to using Stopwatch. It's not like the value is continually updated. It's lazily evaluated (i.e. Ticks is updated when it's referenced).

Documentation for Stopwatch says: "TheStopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time." If you look up info on the high-resolution performance counter, you'll see that it doesn't depend on the system time

Timers are similar. System.Threading.Timer is based on Windows Timer Queue Timers. See that documentation. System.Timers.Timer is just a wrapper around System.Threading.Timer. System.Windows.Forms.Timer is a wrapper around the Windows SetTimer and KillTimer functions. Documentation for those indicates that they are not dependent on the system time.

like image 26
Steven Jeuris Avatar answered Oct 07 '22 06:10

Steven Jeuris