Click here to Skip to main content
15,888,053 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Timer is not a steady solution when used for short periods of time. When i set its interval to 10ms , timer didn't tick for every 10 ms. So is there another solution to do something periodically? Can i use stopwatch instead of timer?

Thanks,


Dcan.
Posted
Comments
Ed Nutting 31-Oct-12 18:18pm    
System.Threading.Timer instead of Windows.Forms.Timer? Threading one is more reliable and works at faster tick rates but has the disadvantage of not running on the UI thread. (It is more reliable/works faster because it isn't on the UI thread but that is more complex than you really need to know...)

Hope this helps,
Ed
Sergey Alexandrovich Kryukov 31-Oct-12 20:03pm    
Agree; I put it in my solution earlier. But we both forget yet another possibility: MM timers, the article referenced in Solution 2.
--SA

Yes, you can use a multimedia timer for more accurate timing.  The standard Windows Forms timer runs at a low priority and is only guaranteed to tick at a time greater than (or equal to) the period.  See this[^] article for how to use the multimedia timer in managed code.

/ravi
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 31-Oct-12 20:05pm    
Good point, but it would need P/Invoke or mixed/mode C++/CLI project, a certain sacrifice in portability. Also, not clear why did you mentioned System.Windows.Forms.Timer specifically. There are at least three more much better timers (please see my answer). (I voted 4 for this answer.)
Cheers,
--SA
Ravi Bhavnani 31-Oct-12 20:45pm    
Quite right, Sergey - I (possibly incorrectly) assumed the OP had a need for a timer in UI app.

/ravi
Sergey Alexandrovich Kryukov 31-Oct-12 21:08pm    
Right, I know three more: System.Threading.Timer, System.Timers.Timer, System.Windows.Threading.DispatcherTimer. Not sure, maybe there are more in FCL :-) How about that?
But MM timer is a good point.
--SA
No, this is not a timer, because you don't have timer events with this class. You can use it with thread in a cycle though. Here is the best alternatives to timers, by the way: a thread with a loop and a call to Thread.Sleep in the cycle. Speaking of the timers: there are at least four timer classes in .NET; and the most important rule is: don't use Systems.Windows.Forms.Timer — some rare exclusions apply: period is very long, no requirements for accuracy, ever, and the developer is barely familiar with programming, does not know invocation techniques and unable to work in a thread safety way. :-)

Timer is generally a bad thing; you should try to avoid them by all means.

As to the good accuracy for short time intervals, there is one simple rule: Windows is not a real-time system, cannot guarantee such things in principle.

Good luck,
—SA
 
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