gpt4 book ai didi

c# - 任务调度程序运行任务的时间过早几毫秒

转载 作者:行者123 更新时间:2023-11-30 14:51:41 24 4
gpt4 key购买 nike

我有一个带触发器的任务:

At 0:00 every day - after triggered, repeat every 1 hours for a duration of 1 day.

在我的应用程序中,我是这样读取时间的:

dateTimeUtcNow = DateTime.Now;

有时,在极少数情况下,dateTimeUtcNow 会显示整点前几毫秒的时间,如 2015-11-11 14:59:59,914

服务器在 Windows Server 2012 R2 上运行,我可以接受家庭版,但不能接受生产版。

为什么?这是一个错误吗?我怎样才能避免这种情况?

最佳答案

查看 Eric Lippert 的文章,了解 DateTime 的准确性,或者说缺乏准确性。 Link here.

文章的关键段落:

In short, the question “what time is it?” really should only be answered to a level of precision that reflects the level of accuracy inherent in the system. Most computer clocks are not accurately synchronized to even within a millisecond of official time, and therefore precision beyond that level of accuracy is a lie. It is rather unfortunate, in my opinion, that the DateTime structure does surface as much precision as it does, because it makes it seem like operations on that structure ought to be accurate to that level too. But they almost certainly are not that accurate.

如果您真的需要计时器在午夜之前关闭而不是更早,那么您将不得不强制使用“稍微” future 的日期/时间,正如评论中已经建议的那样。您实际上没有比这更多的控制权。

关于c# - 任务调度程序运行任务的时间过早几毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33768081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com