How to set timer resolution to 0.5 ms?

I want to set the machine timer resolution to 0.5 ms.

The Sysinternal utility reports that the minimum clock resolution is 0.5 ms, so this can be done.

PS I know how to set it to 1 ms.

PPS I changed it from C # to a more general question (thanks to Hans)

System Timer Resolution

+4
source share
6 answers

NtSetTimerResolution

Code example:

#include <windows.h> extern "C" NTSYSAPI NTSTATUS NTAPI NtSetTimerResolution(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution); ... ULONG currentRes; NtSetTimerResolution(5000, TRUE, &currentRes); 

Link with ntdll.lib .

+7
source

The best you can get from the Win32 API is a millisecond with timeBeginPeriod and timeSetEvent. Maybe your HAL can do better, but this academic one, you cannot write device driver code in C #.

+3
source

You can get a resolution of 0.5 ms using the hidden NtSetTimerResolution() API. NtSetTimerResolution is exported by the native NTDLL.DLL library for Windows NT. See How to set timer resolution to 0.5 ms? on MSDN. However, the true achievable resolution is determined by the underlying equipment. Modern equipment supports a resolution of 0.5 ms. For more information, see Inside Windows NT High Resolution Timers . Supported permissions can be obtained by calling NtQueryTimerResolution ().

How to do:

 #define STATUS_SUCCESS 0 #define STATUS_TIMER_RESOLUTION_NOT_SET 0xC0000245 // after loading NtSetTimerResolution from ntdll.dll: // The requested resolution in 100 ns units: ULONG DesiredResolution = 5000; // Note: The supported resolutions can be obtained by a call to NtQueryTimerResolution() ULONG CurrentResolution = 0; // 1. Requesting a higher resolution // Note: This call is similar to timeBeginPeriod. // However, it to to specify the resolution in 100 ns units. if (NtSetTimerResolution(DesiredResolution ,TRUE,&CurrentResolution) != STATUS_SUCCESS) { // The call has failed } printf("CurrentResolution [100 ns units]: %d\n",CurrentResolution); // this will show 5000 on more modern platforms (0.5ms!) // do your stuff here at 0.5 ms timer resolution // 2. Releasing the requested resolution // Note: This call is similar to timeEndPeriod switch (NtSetTimerResolution(DesiredResolution ,FALSE,&CurrentResolution) { case STATUS_SUCCESS: printf("The current resolution has returned to %d [100 ns units]\n",CurrentResolution); break; case STATUS_TIMER_RESOLUTION_NOT_SET: printf("The requested resolution was not set\n"); // the resolution can only return to a previous value by means of FALSE // when the current resolution was set by this application break; default: // The call has failed } 

Note. The functionality of NtSetTImerResolution is mainly mapped to the timeBeginPeriod and timeEndPeriod functions using the bool Set value (see Inside Windows NT High Resolution Timers for more information about the scheme and all its consequences). However, the multimedia package limits the granularity to milliseconds, and NtSetTimerResolution allows you to set values ​​in milliseconds.

+3
source

You will need a high resolution timer. Here you can find information: http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx

EDIT: additional information can be found here: set to the tenth millisecond; http://msdn.microsoft.com/en-us/library/aa964692(VS.80).aspx

+1
source

If you are using python, I wrote a library called wres that calls inside NtSetTimerResolution.

pip install wres

 import wres # Set resolution to 0.5 ms (in 100-ns units) # Automatically restore previous resolution when exit with statement with wres.set_resolution(5000): pass 
0
source

Timer.Interval = 500;

-6
source

Source: https://habr.com/ru/post/1314206/


All Articles