The normal timers (system.timers.timer) are not accurate on fast timings. The thread sleep instruction timer also is often not. A minimum resolution may be 15mS or it may 55mS. Also it seems that if you specify a time of say 10mS, this will get rounded up to match the nearest time resolution – basically crap! A good resource on this: http://www.codeproject.com/KB/cs/LP_TimerTest.aspx
Pausing In A Function
See sleep in threads
Global Timers
time_since_start = (clock_timer – start_at_time_value);
Not so accurate timeout timer
Note that this timer has a worst case resolution of 500mS so not good for fast timing requirements
int StartTime;
StartTime = System.Environment.TickCount;
while (1)
{
System.Windows.Forms.Application.DoEvents(); //(Neded in case what your waiting for occurs on same thread)
if ((System.Environment.TickCount - StartTime) > 500) //<< Set mS timeout value
{
//TIMED OUT
}
}
Multimedia timers
Allow applications to schedule timer events with the greatest resolution (or accuracy) possible for the hardware platform. These timer services are useful for applications that demand high-resolution timing. For example, a MIDI sequencer requires a high-resolution timer because it must maintain the pace of MIDI events within a resolution of 1 millisecond.
Each multimedia Timer instance runs in its own thread; it doesn’t matter which thread the Timer was created in.
Warning – this type of timer has the potential to cause problems as its so powerful. If your calling it very fast then ensure the function that is called every roll over is fast.
The Win32 multimedia timer is not part of the .NET Framework. However, by using the .NET interoperability services, the multimedia timer can be brought into the .NET fold.
Note that MSDN says this type of timer is obsolete and to use another timer. However, without this timer there is no other timer that allows you to create events with this level of accuracy. The only other accurate timer is the stopwatch class but that can’t be used to create events. Therefore it makes sense to use this even with the obsolete notice as its not obsolete yet and they will need to provide something instead if they do obsolete it.
Int Note – We have a class that provides this timer for the Int Circ Array Tracker Range Finder project. Lots of time spent researching. These we’re the 2 very helpful links code is based on:
http://www.codeguru.com/forum/showthread.php?t=385546
http://www.codeproject.com/KB/cs/LP_TimerTest.aspx
http://www.codeproject.com/KB/miscctrl/lescsmultimediatimer.aspx