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

Facebook - Control privacy setting of liked page

By default, Facebook displayed your liked page to public. It is the user responsibility to adjust the privacy setting. And in my opinion, F...