其中定时器是依赖于系统时间? [英] Which timers are dependent on system time?

查看:370
本文介绍了其中定时器是依赖于系统时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有测试过这个呢。我希望有人已经知道了答案,所以我没有写一个测试应用程序,否则,我会的。 :)

I haven't tested this yet. I am hoping someone already knows the answer, so I don't have to write a test application, otherwise I will. :)

通常,当我想比较的时候,我只储存 DateTime.Now ,并在以后的时间进行比较。我相信,这给错误的结果,当用户更改用户的系​​统时间介于两者之间。

Usually when I want to compare time, I just store DateTime.Now and compare it at a later time. I believe this gives wrong results when the user changes the user's system time somewhere in between.

这使我想知道定时器是否行为相同的方式。在.NET中的定时器是依赖于一套系统时间?考虑开始这就需要在一小时内,以经过一个计时器。它会触发,当我设置了系统一小时的时间向前?

It made me wonder whether timers behave the same way. Which .NET timers are dependent on the set system time? Consider starting a timer which needs to elapse in one hour. Will it trigger when I set system time one hour forward?

我从来没有真正关心这个可能的行为,但它可能在某些情况下非常重要。其中计时器是安全的,不会这种情况下,哪些最绝的是不是?

I never really cared about this possible behavior, but it might be important in some scenarios. Which timers are safe against this scenario, and which ones most definitely aren't?

推荐答案

我就<一href="http://stackoverflow.com/questions/5128027/which-timers-are-dependent-on-system-time#comment5753163_5128027">报价吉姆·米契尔评论,因为它是最相关的回答我的问题。

I'll just quote Jim Mischel comments, as it's the most relevant answer to my question.

无定时器的依赖系统时间。即,用户   改变时钟不会影响 System.Windows.Forms.Timer ,    System.Timers.Timer的 System.Threading.Timer 。它也不会影响   秒表 Environment.TickCount 。另外,有没有额外开销来   使用秒表。它不象价值在不断更新。其   懒洋洋地评估(即时,它的引用被更新)。

None of the timers depend on the system time. That is, the user changing the clock will not affect System.Windows.Forms.Timer, System.Timers.Timer, or System.Threading.Timer. Nor will it affect Stopwatch or Environment.TickCount. Also, there's no "overhead" to using Stopwatch. It's not like the value is continually updated. It's lazily evaluated (i.e. Ticks is updated when it's referenced).

有关 秒表文档 表示:过去秒表测量时间   通过计算计时器滴答底层定时器机制。如果   安装的硬件和操作系统支持高分辨率   性能计数器,那么<​​code>秒表类使用计数器   测量经过的时间。如果你查找信息的高分辨率   性能计数器,你会看到,它不依赖于系统   时间

Documentation for Stopwatch says: "TheStopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time." If you look up info on the high-resolution performance counter, you'll see that it doesn't depend on the system time

计时器是相似的。 System.Threading.Timer 是基于Windows计时器   队列计时器。请参阅文件。 System.Timers.Timer的仅仅是一个   包装器 System.Threading.Timer System.Windows.Forms.Timer 是一个   包装器在Windows 的SetTimer KillTimer函数功能。   文档那些表明它们是不依赖于   系统时间。

Timers are similar. System.Threading.Timer is based on Windows Timer Queue Timers. See that documentation. System.Timers.Timer is just a wrapper around System.Threading.Timer. System.Windows.Forms.Timer is a wrapper around the Windows SetTimer and KillTimer functions. Documentation for those indicates that they are not dependent on the system time.

这篇关于其中定时器是依赖于系统时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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