I want to set a machine timer resolution to 0.5ms.
Sysinternal utility reports that the min clock resolution is 0.5ms so it can be done.
P.S. I know how to set it to 1ms.
P.P.S. I changed it from C# to more general question (thanks to Hans)
System timer resolution
The best you can get out the Win32 API is one millisecond with timeBeginPeriod and timeSetEvent. Maybe your HAL can do better but that's academic, you cannot write device driver code in C#.
You may obtain 0.5 ms resolution by means of the hidden API
NtSetTimerResolution()
. NtSetTimerResolution is exported by the native Windows NT library NTDLL.DLL. See How to set timer resolution to 0.5ms ? on MSDN. Nevertheless, the true achievable resoltion is determined by the underlying hardware. Modern hardware does support 0.5 ms resolution. Even more details are found in Inside Windows NT High Resolution Timers. The supported resolutions can be obtained by a call to NtQueryTimerResolution().How to do:
Note: The functionality of NtSetTImerResolution is basically mapped to the functions
timeBeginPeriod
andtimeEndPeriod
by using the bool valueSet
(see Inside Windows NT High Resolution Timers for more details about the scheme and all its implications). However, the multimedia suite limits the granularity to milliseconds and NtSetTimerResolution allows to set sub-millisecond values.If you use python, I wrote a library named wres, which calls NtSetTimerResolution internally.
pip install wres
Timer.Interval = 500;
NtSetTimerResolution
Example code:
Link with
ntdll.lib
.You'll need a high resolution timer for it.. You can find some information here: http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx
EDIT: More information can be found here: set to tenth of millisecond; http://msdn.microsoft.com/en-us/library/aa964692(VS.80).aspx