当Windows系统时间从NTP时间更新变化时是否有事件? [英] Is there an event when windows system time changes from an NTP time update?

查看:348
本文介绍了当Windows系统时间从NTP时间更新变化时是否有事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的C#应用​​程序中的Windows系统日期/时间更改时,我想收到通知。我发现了 SystemEvents.TimeChanged 事件,MSDN文档说明:


当用户更改系统时钟的时间时发生。


如果时钟被NTP更改,这个事件是否也会被触发?还是还有另一个可以订阅的活动?



实施细节:



我想要一个timer.Elapsed事件在特定的日期/时间触发。我的代码计算使用定时器所需的时间间隔。如果系统时间发生变化,则需要对该计算进行重新评估,以计算新的间隔时间并更新计时器。



答案摘要



感谢所有评论。我以下总结了以下参考资料:




  • SystemEvents.TimeChanged在用户更改时触发,当系统调用与NTP的重新同步时服务器

  • 如果在Windows服务中运行,则需要处理消息泵以获取TimeChanged事件。这可以通过运行调度程序来实现。

  • NTP服务器不应用离散的系统日期更改。考虑到计算的时钟漂移和本地时钟与其他NTP服务器之间的偏移量,逐步应用NTP时钟调整。

  • 对于我的实现,最好定期评估定时器间隔考虑到任何系统时间的变化,而不是设置间隔一次,并将其保持点火。


解决方案

是的,当您配置Windows使用NTP服务器来保持时间更新以及有人手动更改时间时,会引发TimeChanged 。 p>

这里有一个注意事项是你必须有一个UI。 (或启动一个隐藏的窗口来获取消息泵)。即您无法在Windows服务中获取TimeChanged事件。


I'd like to get notified when Windows system date/time is changed in my C# app. I've found the SystemEvents.TimeChanged event, for which the MSDN documentation says:

Occurs when the user changes the time on the system clock.

Does this event also get fired if the clock is changed by NTP? Or is there another event I can subscribe to?

Implementation details:

I want a timer.Elapsed event to fire at a specific date/time. My code calculates the interval required to use for the timer. If there's a system time change, this calculation needs to be re-evaluated to work out the new interval and update the timer.

Summary of answers

Thanks for all the comments. I've summarised them below for future reference:

  • SystemEvents.TimeChanged fires on user change and when the system calls for a resync with the NTP server
  • If running in a windows service, the message pump needs to be processed to get the TimeChanged event. This can be achieved by running a dispatcher.
  • The NTP server doesn't apply a discrete system date change. NTP clock adjustment are applied gradually, taking into account the calculated clock drift and the offset between the local clock and other NTP servers
  • For my implementation, it would be better to evaluate the timer interval periodically to take into account any system time changes, rather than setting the interval once and leaving it to fire.

解决方案

Yes, TimeChanged is raised when you configure Windows to use an NTP server to keep your time up to date as well as when someone manually changes the time.

One caveat here is that you must have a UI. (or start a hidden window that to get a message pump). I.e. you can't get a TimeChanged event in a Windows service.

这篇关于当Windows系统时间从NTP时间更新变化时是否有事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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