27087

Timer minimum limitation

Question:

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?

Answer1:

<strong><em>1 ms or 0.5 ms?</em></strong>

Hans is right, the multimedia timer interface is able to provide down to 1 ms resolution. See <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd742877%28v=vs.85%29.aspx" rel="nofollow">About Multimedia Timers</a> (MSDN), <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd743626%28v=vs.85%29.aspx" rel="nofollow">Obtaining and Setting Timer Resolution</a> (MSDN), and <a href="https://stackoverflow.com/a/11743614/1504523" rel="nofollow">this</a> answer for more details about timeBeginPeriod. Note: Don't forget to call the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd757626%28v=vs.85%29.aspx" rel="nofollow">timeEndPeriod</a> to switch back to the default timer resolution when done.

<strong>How to do:</strong>

#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 <strong>may obtain 0.5 ms</strong> resolution by means of the hidden API NtSetTimerResolution(). NtSetTimerResolution is exported by the native Windows NT library NTDLL.DLL. See <a href="http://social.msdn.microsoft.com/Forums/en-US/8aea13d9-35f1-4b66-a5b4-726651d985f7/how-to-set-timer-resolution-to-05ms-?forum=netfxtoolsdev" rel="nofollow">How to set timer resolution to 0.5ms ?</a> 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 <a href="http://mirrors.arcadecontrols.com/www.sysinternals.com/Information/HighResolutionTimers.html" rel="nofollow">Inside Windows NT High Resolution Timers</a>.

<strong>How to do:</strong>

#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 <strong>and</strong> timeEndPeriod by using the bool value Set (see <a href="http://mirrors.arcadecontrols.com/www.sysinternals.com/Information/HighResolutionTimers.html" rel="nofollow">Inside Windows NT High Resolution Timers</a> 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.

Answer2:

You can use MicroLibrary for this. Take a look at: <a href="http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer" rel="nofollow">http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer</a>

Recommend

  • add a tab programically, c# tab control
  • What does Queue() function do in Chisel?
  • Convert LPWSTR * array of string pointers to C#
  • Python - Creating a dictionary using List
  • How do I apply XACML rules to every child URI?
  • Leading Zero on Single Digits (Flash)
  • java.lang.NoClassDefFoundError for org/springframework/aop/framework/AbstractAdvisingBeanPostProcess
  • Connect to strongest access point (access point with the strongest signal) with the same SSID
  • iText 7: How to build a paragraph mixing different fonts?
  • WebGL: Access buffer from shader
  • Can I convert bool in model to yes/no in a QComboBox
  • Why is RAM in powers of 2?
  • Entity framework 'include' including more than it should
  • glpk.LPX backward compatiblity?
  • Insert statement into SQL Server database
  • How to run Java in compatibility mode?
  • Using same onClick listener with more than one view
  • TypeError: Main.login is not a function
  • C++ boost::spirit parsing embedded languages
  • WPF: can I inherit a UserControl but provide no XAML?
  • How to Set wallpaper of image with its actual size?
  • UI-GRID column summation on checkbox change
  • Objective C IBOutlets
  • Simple Factory with reflection C#
  • Emit new line in Inno Setup preprocessor
  • How to work with AMMediaType for video filters
  • AVCaptureSession VS UIImagePickerController camera preview
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • How to unpack 32bit integer packed in a QByteArray?
  • Hide HTML elements without javascript, only CSS
  • Copy to all folders batch file?
  • Moving mysql files across servers
  • Why is the size of this struct 32?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass