计时器还有其他选择吗?我可以像计时器一样使用秒表吗? [英] Is there another option for timer? Can i use stopwatch like timer?

查看:164
本文介绍了计时器还有其他选择吗?我可以像计时器一样使用秒表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在短时间使用定时器时,它不是一个稳定的解决方案.当我将其间隔设置为10ms时,计时器不会每隔10 ms滴答一次.那么是否存在另一种解决方案来定期执行某项操作?我可以用秒表代替计时器吗?

谢谢,


Dcan.

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.

推荐答案

是的,您可以使用多媒体计时器来获得更准确的计时.标准的Windows Forms计时器以较低的优先级运行,并且仅保证在大于(或等于)时间段的时间进行计时.参见 [ ^ ]文章,介绍如何在托管代码中使用多媒体计时器.

/ravi
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


否,这不是计时器,因为您没有此类的计时器事件.不过,您可以在一个循环中将其与线程一起使用.顺便说一下,这是计时器的最佳替代方法:带循环的线程和循环中对Thread.Sleep的调用.说到计时器:.NET中至少有四个计时器类;最重要的规则是:请勿使用Systems.Windows.Forms.Timer-适用一些罕见的排除方法:期限很长,对准确性没有任何要求,并且开发人员几乎不熟悉编程,不了解调用技术并且无法以线程安全的方式工作. :-)

计时器通常是一件坏事;您应该尽一切可能避免它们.

关于短时间间隔的良好准确性,有一个简单的规则:Windows不是实时系统,原则上不能保证这种事情.

祝你好运,
—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


这篇关于计时器还有其他选择吗?我可以像计时器一样使用秒表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆