Microsoft already says, in the documentation for GetTickCount, that you could never compare tick counts to check if an interval has passed. e.g.:
Incorrect (pseudo-code):
DWORD endTime = GetTickCount + 10000; //10 s from now
...
if (GetTickCount > endTime)
break;
The above code is bad because it is suceptable to rollover of the tick counter. For example, assume that the clock is near the end of it's range:
endTime = 0xfffffe00 + 10000
= 0x00002510; //9,488 decimal
Then you perform your check:
if (GetTickCount > endTime)
Which is satisfied immediatly, since GetTickCount
is larger than endTime
:
if (0xfffffe01 > 0x00002510)
Instead you should always subtract the two time intervals:
DWORD startTime = GetTickCount;
...
if (GetTickCount - startTime) > 10000 //if it's been 10 seconds
break;
Looking at the same math:
if (GetTickCount - startTime) > 10000
if (0xfffffe01 - 0xfffffe00) > 10000
if (1 > 10000)
Which is all well and good in C/C++, where the compiler behaves a certain way.
But when i perform the same math in Delphi, with overflow checking on ({Q+}
, {$OVERFLOWCHECKS ON}
), the subtraction of the two tick counts generates an EIntOverflow exception when the TickCount rolls over:
if (0x00000100 - 0xffffff00) > 10000
0x00000100 - 0xffffff00 = 0x00000200
What is the intended solution for this problem?
Edit: i've tried to temporarily turn off OVERFLOWCHECKS
:
{$OVERFLOWCHECKS OFF}]
delta = GetTickCount - startTime;
{$OVERFLOWCHECKS ON}
But the subtraction still throws an EIntOverflow
exception.
Is there a better solution, involving casts and larger intermediate variable types?
Another SO question i asked explained why {$OVERFLOWCHECKS}
doesn't work. It apparently only works at the function level, not the line level. So while the following doesn't work:
{$OVERFLOWCHECKS OFF}]
delta = GetTickCount - startTime;
{$OVERFLOWCHECKS ON}
the following does work:
delta := Subtract(GetTickCount, startTime);
{$OVERFLOWCHECKS OFF}]
function Subtract(const B, A: DWORD): DWORD;
begin
Result := (B - A);
end;
{$OVERFLOWCHECKS ON}
How about a simple function like this one?
function GetElapsedTime(LastTick : Cardinal) : Cardinal;
var CurrentTick : Cardinal;
begin
CurrentTick := GetTickCount;
if CurrentTick >= LastTick then
Result := CurrentTick - LastTick
else
Result := (High(Cardinal) - LastTick) + CurrentTick;
end;
So you have
StartTime := GetTickCount
...
if GetElapsedTime(StartTime) > 10000 then
...
It will work as long as the time between StartTime and the current GetTickCount is less than the infamous 49.7 days range of GetTickCount.
I have stopped doing these calculations everywhere after writing a few helper functions that are called instead.
To use the new GetTickCount64()
function on Vista and later there is the following new type:
type
TSystemTicks = type int64;
which is used for all such calculations. GetTickCount()
is never called directly, the helper function GetSystemTicks()
is used instead:
type
TGetTickCount64 = function: int64; stdcall;
var
pGetTickCount64: TGetTickCount64;
procedure LoadGetTickCount64;
var
DllHandle: HMODULE;
begin
DllHandle := LoadLibrary('kernel32.dll');
if DllHandle <> 0 then
pGetTickCount64 := GetProcAddress(DllHandle, 'GetTickCount64');
end;
function GetSystemTicks: TSystemTicks;
begin
if Assigned(pGetTickCount64) then
Result := pGetTickCount64
else
Result := GetTickCount;
end;
// ...
initialization
LoadGetTickCount64;
end.
You could even manually track the wrap-around of the GetTickCount()
return value and return a true 64 bit system tick count on earlier systems too, which should work fairly well if you call the GetSystemTicks()
function at least every few days. [I seem to remember an implementation of that somewhere, but don't remember where it was. gabr posted a link and the implementation.]
Now it's trivial to implement functions like
function GetTicksRemaining(...): TSystemTicks;
function GetElapsedTicks(...): TSystemTicks;
function IsTimeRunning(...): boolean;
that will hide the details. Calling these functions instead of calculating durations in-place serves also as documentation of the code intent, so less comments are necessary.
Edit:
You write in a comment:
But like you said, the fallback on Windows 2000 and XP to GetTickCount still leaves the original problem.
You can fix this easily. First you don't need to fall back to GetTickCount()
- you can use the code gabr provided to calculate a 64 bit tick count on older systems as well. (You can replace timeGetTime()
with GetTickCount)
if you want.)
But if you don't want to do that you can just as well disable range and overflow checks in the helper functions, or check whether the minuend is smaller than the subtrahend and correct for that by adding $100000000 (2^32) to simulate a 64 bit tick count. Or implement the functions in assembler, in which case the code doesn't have the checks (not that I would advise this, but it's a possibility).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With