Environment.TickCount是否受系统时间调整影响?

发布于 2024-08-13 22:37:50 字数 725 浏览 5 评论 0原文

我很好奇.NET BCL 属性Environment.TickCount 是如何实现的。我现在特别想知道它是否受到 的影响系统时间调整

我对该属性如何实现的第一个猜测是它只是一个围绕 GetTickCount 方法。但是, GetTickCount 方法的文档指出它受到 GetSystemTimeAdjustment 函数所做调整的影响,但 Environment.TickCount 没有提及任何有关时间调整的内容。

我试图弄清楚是否可以将 Environment.TickCount 用作(尽管精度较低)持续增加的时间值。

I'm curious as to how the .NET BCL property Environment.TickCount is implemented. In particular I'd like to now if it is affected by system time adjustments.

My first guess as to how the property was implemented was that it was simply a managed wrapper around the GetTickCount method. However, the documentation for the GetTickCount method states that it is affected by adjustments made by the GetSystemTimeAdjustment function but the documentation for Environment.TickCount does not mention anything about time adjustments.

I'm trying to figure out if the Environment.TickCount can be used as a (albeit low-precision) consistently increasing time value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-08-20 22:37:50

否,Environment.TickCount 不受系统时间调整的影响。这是我对文档的解释,但我的好奇心需要更严格的证明,因此我运行了以下代码,同时来回调整系统一小时:

while (true)
{
    Console.WriteLine(Environment.TickCount);
    Thread.Sleep(1000); 
}

...输出显示了一个完美的序列,不受时间调整的影响。

更新
因此,我做了一些更多的作业,由下面评论中马库斯的问题引发。我非常确定(但无法确认)Environment.TickCount 会调用 GetTickCount 在文档中提到了以下内容:

GetTickCount的分辨率
功能受限于分辨率
系统定时器,即
通常在 10 范围内
毫秒到 16 毫秒。这
GetTickCount 的分辨率
功能也受到影响
作出的调整
GetSystemTimeAdjustment函数。

因此,虽然它不受更改系统时间的影响,但似乎受到调用SetSystemTimeAdjustment

No, Environment.TickCount is not affected by adjustments of the system time. That was my interpretation of the documentation, but my curiosity demanded harder proof, so I had the following code run while adjusting the system back and forth one hour:

while (true)
{
    Console.WriteLine(Environment.TickCount);
    Thread.Sleep(1000); 
}

...and the output showed a perfect sequence that was not affected by the time adjustments.

Update
So I did some more homework, triggered by Marcus' question in the comments below. I am quite sure (could not confirm though) that Environment.TickCount makes a call to GetTickCount that has the following mentioned in the docs:

The resolution of the GetTickCount
function is limited to the resolution
of the system timer, which is
typically in the range of 10
milliseconds to 16 milliseconds. The
resolution of the GetTickCount
function is also affected by
adjustments made by the
GetSystemTimeAdjustment function.

So, while it is not affected by altering the system time, it seems that will be affected by adjustments that are caused by a call to SetSystemTimeAdjustment.

寻梦旅人 2024-08-20 22:37:50

不会,它不受系统时间调整的影响。

是否适合“持续增加的时间价值”取决于您的具体要求。来自 MSDN 文档

该属性的值是导出的
来自系统计时器并存储为
32 位有符号整数。最后,
如果系统连续运行,
TickCount 将从零增加到
Int32.MaxValue 大约
24.9天,然后跳转到Int32.MinValue,这是一个
负数,然后向后递增
在接下来的 24.9 天内为零。

No, it's not affected by system time adjustments.

Whether it's suitable for a "consistently increasing time value" depends on your exact requirements. From the MSDN documentation:

The value of this property is derived
from the system timer and is stored as
a 32-bit signed integer. Consequently,
if the system runs continuously,
TickCount will increment from zero to
Int32.MaxValue for approximately
24.9 days, then jump to Int32.MinValue, which is a
negative number, then increment back
to zero during the next 24.9 days.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文