如何以每秒的TOP时间运行计时器? (当毫秒= 000时) [英] How to run a timer at the TOP of each second? (when millis = 000)

查看:131
本文介绍了如何以每秒的TOP时间运行计时器? (当毫秒= 000时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用了System.Threading.Timer的数量,并将此计时器设置为每1秒触发一次.我的应用程序每1秒钟执行一次线程,但是执行的毫秒数有所不同.我的输出如下:

In my application, I have used the number of System.Threading.Timer and set this timer to fire every 1 second. My application execute the thread at every 1 second but it execution of the millisecond is different. My output is given below:

10:28:32.875
10:28:33.390
10:28:34.875
....
10:28:39.530
10:28:40.875


但是,我想要以下结果:


However, I want following results:

10:28:32.000
10:28:33.000
10:28:34.000
....
10:28:39.000
10:28:40.000


推荐答案

我认为几乎不可能计时器滴答事件将在1秒后完全触发.其背后的原因是 windows 不是实时操作系统.对于Windows等多线程操作系统,计时器本质上是不精确的.

让我解释一下.
例如,在第一次启动时,计时器正好在1秒后触发,现在是第二次触发该事件时,假设操作系统正忙于执行其他操作,那么从该进程切换到计时器进程显然会花费一些时间.在这种情况下说10毫秒过去了.因此很明显,您的计时器将延迟10毫秒触发.

但是并非完全不可能获得准确度.您可以安排一些硬件来处理计时器事件.

希望这会有所帮助.
最好.
I think it is almost impossible to get the timer tick event to fire exactly after 1 sec. The reason behind it is, windows is not a realtime operating system. And Timers are by nature inexact when it comes to multithreaded operating system like windows.

Let me explain you.
e.g At the very first start Timer firs exactly after 1 sec, now when it comes to firing the event second time, suppose operating system is busy executing something else, then obviously it will take some time to switch from that process to the timer process. In this scenario say 10 ms are passed. So obviously your timer will fire 10 ms late.

But it is not totally impossible to get the accuracy. You can have some hardware arrangement to take of the timer event.

Hope this helps.
All the best.


计时器只会给您经过的时间,最多只能提供15ms的分辨率.

因此,一个1秒的计时器事件是每秒1秒+-15毫秒.

您总是可以截断毫秒数而不显示它们.
Timers only give you elapsed times and at best to a 15ms resolution.

So a 1 sec timer event is 1 sec +- 15ms every second.

You can always truncate the milliseconds and not show them.


这篇关于如何以每秒的TOP时间运行计时器? (当毫秒= 000时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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