.net计时器的可靠性如何? [英] How reliable are .net timers?

查看:52
本文介绍了.net计时器的可靠性如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在Windows服务中使用System.Timers.Timer.我想知道它们的可靠性和准确性.特别是:

I'm looking at using a System.Timers.Timer in a windows service. I would like to know how reliable and accurate they are. In particular:

  1. 是否可以保证每隔多长时间运行一次?
  2. 处理器或内存过载时会发生什么?在这种情况下,ElapsedEventArgs.SignalTime会始终准确吗?
  3. 如果计时器运行了很长时间,那么考虑到系统时钟的不正确而对系统时间进行校正时会发生什么?

更新:感谢您到目前为止的回答.我希望能够针对我在原始问题中详细介绍的三个问题获得更具体的答案.

Update: Thanks for the answers so far. I was hoping to get more specific answers to the three issues that I detailed in my original question.

推荐答案

对您来说正确的是什么?普通的Windows计时器的精度约为100Hz.如果您想要更准确的信息,请看一下QueryPerformanceTimer(System.Diagnostics.Stopwatch使用它).短时间间隔非常好,而且非常准确,但是随着CPU频率的变化(电源管理),它将不同步.

What is accurate for you? Normal windows timer has accurancy of about 100Hz. If you want something more accurate, take a look at QueryPerformanceTimer (System.Diagnostics.Stopwatch uses it). It is great and very accurate for short time spans, but as CPU frequency changes (power management), it will get out of sync.

此外,我已经看到系统时钟每天漂移几分钟,因此我们不得不安装NTP实用程序并将其计划(具有讽刺意味的)定期运行.

Also, I have seen system clock drift for minutes a day, so we had to install NTP utility and schedule it (the irony) to run periodically .

基本上,即使在某些尝试使用主时钟作为基线而QuerperformanceTimer作为差分计时器的尝试下,我们也从未在Windows机器上实现亚毫秒级的时间精度.永远做不到.

Basically, we never acheived submillisecond time accurancy on windows machine, even with some tries to use main clock as baseline and QuerperformanceTimer as sort of differential timer. Never worked right.

这篇关于.net计时器的可靠性如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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