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

前端 未结 6 1538
粉色の甜心
粉色の甜心 2020-12-18 14:42

I need an accurate timer to interface a Windows application to a piece of lab equipment.

I used System.Timers.Timer() to create a timer that ticks every 10 msec, but

相关标签:
6条回答
  • 2020-12-18 15:01

    Off the top of my head, I could suggest running a thread that mostly sleeps, but when it wakes, it checks a running QueryPerformanceCounter and occasionally triggers your procedure.

    0 讨论(0)
  • 2020-12-18 15:08

    There's a nice write up at the MSDN: Implement a Continuously Updating, High-Resolution Time Provider for Windows

    Here's the sample source code for the article (C++).

    0 讨论(0)
  • 2020-12-18 15:13

    You need to use a high resolution timer such as QueryPerformanceCounter

    0 讨论(0)
  • 2020-12-18 15:13

    On surface of it the answer is something like "high resolution timer" however this is incorrect. The answer requires a regular tick generation and the windows high res performance counter API does not generate such a tick.

    I know this is not answer inself but the popular answer to this question so far is wrong enough for me to feel that a simple comment on it is not enough.

    0 讨论(0)
  • 2020-12-18 15:14

    In System.Diagnostics, you can use the Stopwatch class.

    0 讨论(0)
  • 2020-12-18 15:20

    The limitation is given by the systems heartbeat. This typically defaults to 64 beats/s which is 15.625 ms. However there are ways to modify these system wide settings to achieve timer resolutions down to 1 ms or even to 0.5 ms on newer platforms:

    1. Going for 1 ms resolution by means of the multimedia timer interface (timeBeginPeriod()):

      See Obtaining and Setting Timer Resolution.

    2. Going to 0.5 ms resolution by means of NtSetTimerResolution():

      See Inside Windows NT High Resolution Timers.

    You may obtain 0.5 ms resolution by means of the hidden API NtSetTimerResolution().

    I've given all the details in this SO answer.

    0 讨论(0)
提交回复
热议问题