窗口最小化时.NET Timer 控件运行速度更快
这实际上是一个明显的差异,我已经看到但无法解释。这些计时器的间隔设置为 1 毫秒(可用的最低值),但虽然它被最小化,但它似乎滴答得更快?谁能给我解释一下这个现象吗?如果可能的话,解释一下如何在窗口最大化时重现效果?
It's actually a noticeable difference that I've seen but cannot explain. These timers have intervals set to 1ms (the lowest available), but while it's minimized, it seems to tick faster? Could anyone explain this phenomenon to me? And if possible, explain how to reproduce the effect while the window is maximized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个
Forms.Timer
吗?我怀疑它运行得更快,更有可能是计时器触发事件得到了更及时的处理。虽然最小化,但表单窗口的消息泵处理的消息可能会减少,这可能会占用更大的时间片来处理计时器消息。还有最小定时器分辨率的问题。
如果适用,请尝试使用其他计时器类型之一,例如 System.Timers
参考。
Is this a
Forms.Timer
?I doubt it is running faster, more likely that the Timer firing event is being handled in a more timely manner. Whilst minimised there will presumably be less messages handled by the Form windows's message pump, which could account for a larger time slice to handle the Timer messages. There is also the isue of the minimum Timer resolution.
If applicable, try using one of the other Timer types, such as
System.Timers
Ref.
如果我没记错的话,您可以从 System.Windows.Forms.Timer 中获得的最小分辨率(我假设您在这里使用的是它)是 55 毫秒。将其设置为 1 毫秒本质上意味着它会连续滴答。
当然,计时器并不能保证刻度会准确地按照指定的时间间隔到达。如果您的应用程序正忙于执行其他操作(例如重绘屏幕),那么可能会花费更多的时间,或者在重负载下需要更多的时间。如果计时器设置为 1 秒的间隔,您不会真正注意到这一点,但在最小窗口(55 毫秒)下,您可能会注意到。
当应用程序最小化时,在计时器事件触发之前可以中断计时器事件的其他事件就会减少。
If I remember correctly, the minimum resolution you can get out of a
System.Windows.Forms.Timer
(which I assume is what you're using here) is 55 ms. Setting it to 1 ms essentially means that it ticks continuously.Of course, a timer doesn't guarantee that ticks will arrive at exactly the interval specified. If your app is busy doing other things (like redrawing the screen) then it may take a few more ms, or significantly more under heavy load. If the timer is set to an interval of 1 second, you won't really notice this, but at the minimum window (55 ms), you might.
When the application is minimized, there are fewer other events that can interrupt the timer events before they fire.