CodeMonkey
CodeMonkey

Reputation: 12444

Timer minimum limitation

I need to write a program (.NET C#) which is doing some stuff but every 0.5 ms I need to read some data and see if it changed to a certain value or above it and if that value has reached that goal, stop everything else I'm doing. Is there a problem with setting a timer to run every 0.5 ms? What is the proper approach for this kind of programs?

Upvotes: 0

Views: 2213

Answers (2)

Arno
Arno

Reputation: 5204

1 ms or 0.5 ms?

Hans is right, the multimedia timer interface is able to provide down to 1 ms resolution. See About Multimedia Timers (MSDN), Obtaining and Setting Timer Resolution (MSDN), and this answer for more details about timeBeginPeriod. Note: Don't forget to call the timeEndPeriod to switch back to the default timer resolution when done.

How to do:

#define TARGET_RESOLUTION 1         // 1-millisecond target resolution

TIMECAPS tc;
UINT     wTimerRes;

if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) 
{
   // Error; application can't continue.
}

wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
timeBeginPeriod(wTimerRes); 

// do your stuff here at approx. 1 ms timer resolution

timeEndPeriod(wTimerRes); 

Note: This procedure is available to other processes as well and the obtained resolution applies system wide. The highest resolution requested by any process will be active, mind the consequences.

However, 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 resolution 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.

How to do:

#define STATUS_SUCCESS 0
#define STATUS_TIMER_RESOLUTION_NOT_SET 0xC0000245

// after loading NtSetTimerResolution from ntdll.dll:

ULONG RequestedResolution = 5000;
ULONG CurrentResolution = 0;

// 1. Requesting a higher resolution
if (NtSetTimerResolution(RequestedResolution,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
switch (NtSetTimerResolution(RequestedResolution,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 basically mapped to the functions timeBeginPeriod and timeEndPeriod by using the bool value Set (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.

Upvotes: 3

HABJAN
HABJAN

Reputation: 9338

You can use MicroLibrary for this. Take a look at: http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer

Upvotes: 0

Related Questions