Accurate Windows timer? System.Timers.Timer () limited to 15 ms

I need an accurate timer to associate a Windows application with a piece of laboratory equipment.

I used System.Timers.Timer () to create a timer that ticks every 10 ms, but this clock is slow. For example, 1000 ticks with an interval of 10 ms should take 10 seconds of the wall clock, but in fact it takes more than 20 hours of the wall clock (on my PC). I assume this is because System.Timers.Timer () is an interval timer that reset every time it expires. Since the time between the timer and the reset time (up to 10 ms) will last some time, the clock will work slowly. This is probably fine if the interval is large (seconds or minutes), but not acceptable for very short intervals.

Is there a function in Windows that will run the procedure every time the system clock crosses the 10 ms border (or something else)?

This is a simple console application.

thank

Norm

UPDATE: System.Timers.Timer () is extremely inaccurate for small intervals.

I wrote a simple program that counted 10 seconds in several ways:

Interval = 1, Account = 10000, Operating time = 160 s, ms for an interval = 16

Interval = 10, Account = 1000, Operating time = 16 s, ms for an interval = 15

Interval = 100, Count = 100, Operating time = 11 s, ms for an interval = 110

Interval = 1000, Count = 10, Operating time = 10 s, ms per interval = 1000

It seems that System.Timers.Timer () cannot tick faster than about 15 ms, regardless of the interval setting.

Note that none of these tests seemed to use any measurable processor time, so the limit is not the CPU, but just a .net restriction (bug?)

At the moment, I think I can live with an inaccurate timer that starts the procedure every 15 ms or so, and the procedure gets the exact system time. This is strange, but ...

I also found the ZylTimer.NET shareware product, which claims to be a much more accurate .net timer (1-2 ms resolution). It may be what I need. If there is one product, probably others.

Thanks again.

+2
Jan 15 '09 at 22:33
source share
6 answers

You need to use a high resolution timer like QueryPerformanceCounter

+4
Jan 15 '09 at 22:38
source share

The surface of this answer is a bit of a β€œhigh resolution timer”, however this is not true. The answer requires regular tick generation, and the Windows High Performance Counter API does not generate such a tick.

I know this is not an answer, but the popular answer to this question is still not enough for me to feel that a simple comment on it is not enough.

+4
Jan 15 '09 at 22:56
source share

In System.Diagnostics you can use the Stopwatch class.

+2
Jan 15 '09 at 22:58
source share

The limit is set by the heart rate of the system. Typically, this value is 64 bit / s, which is 15.625 ms. However, there are ways to change these system settings to achieve a timer resolution of up to 1 ms or even up to 0.5 ms on new platforms:

You can get a resolution of 0.5 ms using the hidden NtSetTimerResolution() API.

I have described the SO answer in detail in this .

+2
Apr 05 '14 at 18:35
source share

On top of my head, I can suggest starting a thread that is mostly sleeping, but when it wakes up, it checks the QueryPerformanceCounter is running and sometimes starts your procedure.

+1
Jan 15 '09 at 23:07
source share
+1
Jan 16 '09 at 15:13
source share



All Articles