The while loop uses a SpinWait, this is not a sleep but runs for a few nanoseconds and effectively puts the thread to sleep without relinquishing the remainder of its CPU time slot. It should be noted that MicroTimer is inefficient and very processor hungry as the NotificationTimer function runs a tight while loop until the elapsed microseconds is greater than the next interval. The NotificationTimer function is where the 'work' is done and is run in a separate high priority thread. The timer implements a custom event handler ( MicroTimerElapsedEventHandler) that fires every interval. MicroTimer - Designed so it operates in a very similar way to the class, it has a timer interval in microseconds and Start / Stop methods (or Enabled property).This is useful as a standalone class where the elapsed microseconds from when the stopwatch was started can be directly obtained. MicroStopwatch - This derives from and extends the class importantly, it provides the additional property ElapsedMicroseconds.'MicroLibrary.cs' encompasses three classes (under the namespace MicroLibrary): The MicroTimerEventArgs properties provide information (to the microsecond) on when exactly (and how late) the timer was invoked. On each timed event, MicroTimer invokes the predefined ( OnTimedEvent) callback function. The beauty of MicroTimer is that it is called in a very similar way to the existing class however, the interval is set in microseconds (as opposed to milliseconds in ). The accuracy is highly system/processor dependant a faster system will result in a more accurate timer. The trouble is, the 0.1% of the time, the timer can be very inaccurate (whilst the Operating System gives some of the processing time to other threads and processes). MicroTimer cannot and does not solve this problem however, it does offer a microsecond timer which offers a reasonable degree of accuracy (approx. The fundamental problem with a software timer in the region of 1ms is that Windows is a non real-time Operating System (RTOS) and is not suitable for generating regular and accurate events around the 1ms mark. Basically, what I needed was a microsecond timer that was accurate the majority of the time. The problem I encountered was that I needed to send an Ethernet UDP message packet out every 800µs (0.8ms) it did not matter if a packet was slightly delayed or did not go off exactly 800µs after the last one. NET projects that wrap this timer) however, there are no timers available in the microsecond range. Significantly better performance can be achieved using the Win32 multimedia timer (there are various. The resolution will be system dependant, but a maximum resolution is usually around 15ms ( has an even worse resolution, although it is unlikely a UI will need to be updated this fast). NET class for low interval times will realise that it does not offer a very high resolution.
0 Comments
Leave a Reply. |