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?
-
see http://stackoverflow.com/questions/4532850/windows-forms-timer-or-system-threading-timer – Mitch Wheat Apr 03 '14 at 11:56
-
@MitchWheat it doesn't really answer what I asked. – CodeMonkey Apr 03 '14 at 11:58
-
3Yes that's a problem. Thread scheduling and timer callbacks occurs at clock tick interrupts. Which occur 64 times per second by default, 15.625 msec is thus the minimum resolution you can get. Technically you can increase the interrupt rate but 1 msec is the lowest you can possibly get. Getting to 0.5 msec requires a real-time operating system. – Hans Passant Apr 03 '14 at 12:07
-
@HansPassant How can I get to 1 msec? – CodeMonkey Apr 03 '14 at 13:56
-
Hmm, you suddenly change your requirement by 100%. What do you *really* need? Or are you just picking numbers that sound good? You can pinvoke timeBeginPeriod(). timeSetEvent() provides an accurate callback. – Hans Passant Apr 03 '14 at 14:11
-
I was asked to try doing it in 0.5 msec but 1 msec is also fine if 0.5 is a problem. If you give an answer with code examples, I'll accept it – CodeMonkey Apr 03 '14 at 14:22
2 Answers
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.
You can use MicroLibrary for this. Take a look at: http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer

- 9,212
- 3
- 35
- 59
-
Do you understand what they did in order to solve the problem that thread scheduling and timer callbacks occurs at clock tick interrupts,Which occur 64 times per second by default? That means that 15.625 msec is thus the minimum resolution you can get. – CodeMonkey Apr 03 '14 at 14:03