Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

My purpose is to create a periodical function which the elapse time of OnTimeEvent between two continuous times is exactly equal to the interval of the timer. Then I can use the interval time as sampling time.

What I have tried:

I am using timer elapse event in C# to create a periodical function.
Even though the Timer interval is set to 10ms, but when I measure the elapsed time is varied in the range of 12-30ms, even though there are several lines of code in the OnTimeEvent function (I measure it is about 1-3ms).

How I can do it in C#, should I use Thread task? or another method.
Please suggest me a solution that can solve my problem.

Thank you so much!
Updated 12-Sep-17 12:11pm

1 solution

Tick based timers won't work for small intervals.

From my point of view a thread based timer would be the best solution. You might also have a look at the Multimedia Timers (Windows)[^].

But you should be aware that Windows is not a real time operating system. You will not get constant timer intervals; especially when the system load is high or there are many disk or network data transfers.

Some CP links:
Microsecond and Millisecond C# Timer[^]
The Multimedia Timer for the .NET Framework[^]

An SO thread providing different solutions:
c# - Most accurate timer in .NET? - Stack Overflow[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900