计时器的间隔 - 从整数到浮点/双精度 - Delphi
有没有办法解决 Ttimer 间隔的限制,使其更精确?例如,不要仅使用 1000ms 这样的整数,而是使用 1000.5ms 。如果不是,我可以使用哪个组件来代替,这会给我更精确的间隔
Is there a way to work around the Limits of the Ttimer's inteval so it can be preciser? for example instead of only integers like 1000ms , to use 1000.5ms . And if no, which component can I use instead which will give me preciser interval
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试以合理的准确度记录时间。然而,标准系统定时器不能用于此目的。系统计时器所保证的只是它会在您指定的时间间隔内不早触发。如果您延迟发送消息队列,则可能会延迟收到消息。很简单,系统计时器并不是设计用来用作秒表的,并且尝试这样做是不合适的。
相反,您需要使用高分辨率性能计数器,您可以通过调用
查询性能计数器
。如果您使用的是 Delphi 2010 或更高版本,则可以使用
Diagnostics.TStopwatch
为高性能定时器提供了一个非常方便的包装器。您仍然可以使用系统计时器为您的应用程序提供定期的滴答声或脉冲,但请确保使用高分辨率计时器来跟踪时间。
说了这么多,我不确定您是否能够实现您希望做的事情。如果您想保持相当准确的时间,那么我上面所说的是正确的。尝试与通过网络远程某处的另一台计算机上运行的代码保持锁步同步对我来说听起来非常棘手。
You are trying to keep track of time to a reasonable degree of accuracy. However, the standard system timer cannot be used for that purpose. All that the system timer guarantees is that it will fire no sooner than the interval which you specify. And you can get the message late if you are tardy in pumping your message queue. Quite simply, the system timer is not designed to be used as a stopwatch and to attempt to do so is inappropriate.
Instead you need to use the high resolution performance counter which you can get hold of by calling
QueryPerformanceCounter
.If you are using Delphi 2010 or later then you can use
Diagnostics.TStopwatch
which provides a very convenient wrapper to the high performance timer.You can still use a system timer to give your app a regular tick or pulse, but make sure that you keep track of time with the high resolution timer.
Having said all of that, I'm not sure that you will ever be able to achieve what you are hoping to do. If you want to keep reasonably accurate time then what I say above is true. Trying to maintain lock-step synchronisation with code running on another machine somewhere remote over the net sounds pretty much intractable to me.
1) TTimer 类对于您的任务来说不够准确! (话又说回来,网站计时器也不会)
2) 如果您使用 TimeBeginPeriod() API 调用增加计时器分辨率,这将使您更接近,但仍然不够接近
3) 如果您调整 TTimer每次基于恒定的开始时间(并与 PC 时钟同步)间隔,与 PC 时钟相比,您可以为每个时间事件平均设定的毫秒数
4) 我不知道 TTimer 类是否处理 3)正确,但我有一个等效的 TTimer 功能
5)为了解决 PC 时钟漂移,您需要定期与 NTP 服务器同步 PC 时钟
6)我有一个系统,可以将 PC 时钟保持在一台好的机器上,以 +/ - 永久的 5 毫秒参考时间(我每分钟调整一次)和一个分辨率为 +/- 2 毫秒的计时器,只要机器不超载(Windows 不是实时操作系统)
7)我花了很长时间才能到达这一点——这是你真正需要的,还是你问错了问题?
1) The TTimer class is not accurate enough for your task, period! (then again, neither would the web-site timer be, either)
2) If you increase the timer resolution using the TimeBeginPeriod() API call, this will get you closer, but still nowhere near close enough
3) If you adjust the TTimer interval each time based on a constant start time (and synchronised with the PC clock), you can average a set number of milliseconds for each time event compared to the PC clock
4) I don't know if the TTimer class handles 3) correctly, but I have a TTimer equivalent that does
5) To account for PC clock drift you will need to synchronise the PC clock periodically with an NTP server
6) I have a system that keeps the PC clock on a good machine to with +/- 5 milliseconds of a reference time permanently (I adjust every minute) and a timer with a resolution of +/- 2 milliseconds as long as the machine is not overloaded (Windows is not a real-time OS)
7) It took me a long time to get to this point - is this what you really need, or are you asking the wrong question?