Wednesday, June 16, 2010

C# - Timer Precision Issue

Timer precision issue doesn't only affect Java, it should affect most programming language. It is because when you pause a thread, it gives away its right and allow the OS to schedule their job.

Thus, Thead.Sleep/Monitor.Wait does not guarantee the sleep time.

To archive high resolution, you can try using multimedia timer such as QueryPerformanceCounter . This is good for perform elapse calculations.

Or you can use timeBeginPeriod to requests a minimum resolution for periodic timers.

For example, to reduce the resolution to 1 ms

internal static extern uint timeBeginPeriod(uint period);
internal static extern uint timeEndPeriod(uint period);

Thread.Sleep(1); // will sleep 1ms every time

No comments:

Post a Comment

Tomcat - removing server info

Below are the steps to remove Tomcat Server Information 1. Make sure Tomcat is not running 2. Navigate to "Tomcat Installation"...