简体   繁体   English

如何在C#中检查时间是否手动更改

[英]How to check if time changed manually in C#

I am writing a program that does something on computer periodically based on current time. 我正在编写一个程序,该程序根据当前时间在计算机上定期执行某些操作。 But user may change windows time manually and make my program confounded. 但是用户可能会手动更改Windows时间,并使我的程序感到困惑。 My idea was saving current time in every loop iteration and comparing it with last saved time. 我的想法是在每次循环迭代中节省当前时间,并将其与上次节省的时间进行比较。 So I could determine time changing and the amount of change. 因此,我可以确定时间变化和变化量。 But it makes mistakes. 但这会出错。 System Hibernate (and Sleep) does same thing so I can't make difference between "manually time changing" and "System Hibernate". 系统休眠(和睡眠)执行相同的操作,因此我无法在“手动更改时间”和“系统休眠”之间进行区分。 In fact Now the problem can be solved with determining Hibernate (and Sleep). 实际上,现在可以通过确定休眠(和休眠)来解决问题。 Can you please help me (In determining Hibernate (and Sleep) or determining time changing)? 您能帮我吗(确定休眠(和睡眠)或确定时间变化)?

You could use a Stopwatch to measure elapsed time. 您可以使用Stopwatch来测量经过时间。 The stopwatch does not depend on the system clock and is therefore not affected by changes to it. 秒表不依赖于系统时钟,因此不受更改的影响。

Hibernation, sleep etc. can be detected with the SystemEvents.PowerModeChanged event. 可以使用SystemEvents.PowerModeChanged事件检测到休眠,睡眠等。

Use time web services to check the time on application startup. 使用时间Web服务来检查应用程序启动时间。 Or you can create Windows service that will check for time change even if your program isn't launched. 或者,您可以创建Windows服务,即使您的程序未启动,该服务也会检查时间是否更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM