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

Windows 7 - How to reset Windows 7 password

This is just a post of what I had found in the Internet and it works for me (See reference for the actual post). While I knew that there a...