What's the most precise timer in .NET?

1.5k views Asked by At

I want to use a timer that allows me to run a specific code at certain intervals but Winforms timer is very inaccurate. I set it to 2 seconds but it goes off as much as 48 seconds. The kind of code I am running is highly dependent on acting as soon as possible so 48 seconds is way too late to do anything.

Also I don't want the timer to eat a lot of CPU cycles because the code I am running is very simple.

2

There are 2 answers

0
tinstaafl On BEST ANSWER

Use the System.Timer Class. Since it's designed for multi-threaded environments it's more accurate than the timer control.

1
Mehrdad Kamelzadeh On

Check this as well and also the MSDN comparison is really deep and complete.

By the way the most accurate timer is Stopwatch which is used for calculating the Code Execution times and other similar stuffs and it is used by Visual Studio for calculating the performance of the codes so it is clear how precise it should be. But In your case you can not make use of it. I mentioned StopWatch for your information and the other who might see this post in future.