How reliable are .net timers?

I am looking at using System.Timers.Timer in a windows service. I would like to know how reliable and accurate they are. In particular:

  • Are there any guarantees on how often they will work?
  • What happens when the processor or memory is overloaded? Will ElapsedEventArgs.SignalTime always be accurate in such circumstances?
  • If the timer starts for a long time, what happens when patches in the system time are taken into account by inaccuracies in the system clock?

Update: Thanks for the answers so far. I was hoping to get more specific answers to the three questions that I detailed in my original question.

+4
source share
4 answers

What exactly is for you? A normal window timer has an accuracy of about 100 Hz. If you want something more accurate, take a look at QueryPerformanceTimer (System.Diagnostics.Stopwatch uses it). This is wonderful and very accurate for short periods of time, but as the processor frequency changes (power management) it will go out of synchronization.

In addition, I saw the drift of the system clock for several minutes a day, so we had to install the NTP utility and schedule it (irony) for periodic launch.

Basically, we never observed submillisecond time accuracy on a Windows machine, even if some try to use the main clock as a baseline and QuerperformanceTimer as a kind of differential timer. Never worked correctly.

+2
source

From my experience, I can say that timers can go out of sync if you have very high performance.

+1
source

If you need very high performance in .NET, you should check out System.Diagnostics.Stopwatch. It uses a kernel timer in windows to accurately measure time. It is usually intended for diagnostic purposes, such as measuring elapsed runtime for a given piece of code ... but perhaps it could have been wrapped in a custom high-performance timer class for scheduled events.

+1
source

I am using the value of sleep (Thread.Sleep (int)) during the loop for my windows service tasks. Thus, you can implement calculations for how much you want to sleep, if this is not an interval, but a specific time. There has never been a problem with this approach.

0
source

All Articles