Environment.TickCount returns negative numbers - MSDN doc error
This one just bit us a today.
The documentation for TickCount on MSDN say that this value is always positiove:
<Quote>
The TickCount property handles an overflow condition by resetting its value to zero. The minimum value returned by TickCount is 0.
</Quote>
And in another place in the docs:
<Quote>
The value of this property is derived from the system timer and is stored as a 32-bit signed integer. Therefore, the elapsed time will wrap around to zero if the system is run continuously for 24.9 days.
</Quote>
However, in MSDN2, the docs say:
<Quote>
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.
</Quote>
I remembered the original docs from 1.1 and so never had any reason to doubt the potential values. Apparently, in 1.1 it could go to negative numbers as well, though I have not checked that.
This bug is a female dog to find too - it will only show after 25 days without a reboot.
Comments
Anonymous
August 20, 2006
Thanks for your post. This bug just got me. (VS2003, Compact Framework 1.1).
-ChrisAnonymous
July 09, 2007
Thanks for your post. This bug got me on my production system.Anonymous
November 16, 2007
But we're talking WINDOWS here, how likely is it that a windows machine will run continuously for 24.9 days? Seems like an edge case to me.Anonymous
November 16, 2007
A jab at windows stability! That's fresh. ;)Anonymous
January 12, 2008
Thanks for this information. I was fighting with this weird bug under .NET1.1 for a couple of days until I came across your blog post. I'm very astonished that I cannot find an official bug report or statement from MS. Does anybody know where I can get an official bug report or statement from MS?Anonymous
September 06, 2012
I just encountered the problem using System.Environment.TickCount and the native API function (declared as int instead of uint). For my case, my work PC was powered on more than a month or two ago: I usually hybernate its state. Counting the summer vacations, you can understand how it overflowed the 24 days limit. ;)Anonymous
March 11, 2013
Thanks for the post. This hit me as well. Worst bug I've ever had, I think.Anonymous
October 13, 2014
I've encountered the problem using System.Environment.TickCount and to avoid this bug, you've to remove the sign using following code: int positiveResult = Environment.TickCount & int.MaxValue; If you remove the sign the returned value will be always positive, which causes the application to work even when the TickCount returns negative value. I hope that this will help someone.Anonymous
December 10, 2014
many thanks for the post..i just went weirdo seeing tick-counts all of a sudden negative.