Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 687461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:07:45+00:00 2026-05-14T02:07:45+00:00

Microsoft already says, in the documentation for GetTickCount, that you could never compare tick

  • 0

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)

The solution

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 what about Delphi?

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?


Update

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}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:07:45+00:00Added an answer on May 14, 2026 at 2:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of the things that the Microsoft documentation says about enabling reliable sessions is
Microsoft already has a Windows 7 Beta Customer Preview Program on their MSDN site
Does anybody know if Microsoft has already decided which version of the .NET framework
Microsoft recently announced that the Javascript/HTML DOM library jQuery will be integrated into the
I already added the microsoft reference in. But basically what I want to do
I guess everyone has already heard the news about some key developers leaving the
Question: Today I worked with MS Visual Sourcesafe, that is to say Microsoft's Sourcecode
Microsoft SQL Server Profiler is a graphical user interface to SQL Trace for monitoring
Microsoft's new Windows Live Application Based Storage API is a RESTful API. More info
Microsoft Access is a slick way to access data in a MS SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.