Environment.TickCount 如何在 25 天(或 48-49 天)后溢出

How Environment.TickCount overflows after 25 days (or 48-49 days)

在阅读此问题及其回复时Environment.TickCount is not enough 我一头雾水。

他们说 Environment.TickCount 工作 48-49 天 (OP)(@Cody Gray 说 25 天),但它是一个 int 值,因此最多可以存储 2,147,483,647。

现在,1 毫秒包含 10,000 个刻度,这意味着它最多可以容纳 214,748.3+ 毫秒,也就是 214+ 秒,不到 4 分钟。

我是不是漏掉了什么?

顺便说一句,现在 Environment.TickCount64 提供了 64 位值。

对于初学者来说,TickCount returns 毫秒,而不是 DateTime.Ticks 意义上的滴答。它以与系统计时器相同的精度执行此操作,大约为 10-16 毫秒。

文档解释了值如何循环:

Because the value of the TickCount property value is a 32-bit signed integer, 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. You can work around this issue by calling the Windows GetTickCount function, which resets to zero after approximately 49.7 days, or by calling the GetTickCount64 function.

计算一下,我们发现 24.9 天是 2,151,360,000(与 Int.MaxValue 的 2,147,483,647 相比)。 MaxValue + |MinValue| 产生 4294967295,如果除以 86400000 毫秒(1 天)产生大约 49.7 天,如文档所示。

Source