Results 1 to 16 of 16

Thread: GetTickCount - Ouch!

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    GetTickCount - Ouch!

    When you find out that a function that 95% of your code relies on craps out after 24ish days.

    I didn't know it worked like that.

    The nuts and bolts of it is that my program throws up random events.

    An early problem was sometimes it would throw up the same random event a very short time to the last time.

    So I put timers in them to indicate how long they had to wait before they could occur again. If it got called before that it skipped out.

    Then ALL events suddenly stopped working in my program at the same time.

    When I checked it, I found that GetTickCount was return -2 billion something or other.

    What that meant was that every event called skipped out because the interval was less than the requirement.

    I've fixed it but wow... it's all over my code and now a lot to fix.

    In 30 something years of VB6 I've never run into this before. But I don't think I've ever run any of my code for 24 days straight either.

    Offending Code:

    Code:
    Public Property Get EventElapsedSeconds(ByRef EventStartingTicks As Long) As Double
    
    EventElapsedSeconds = CDbl((GetTickCount - EventStartingTicks)) / 1000
    
    End Property
    Example of how it was called:

    Code:
    Private Function Random05() As Long
    Static nTickcount As Long
    
    If EventElapsedSeconds(nTickcount) < 60 Then Exit Function
    
    ShowGamePlayNotes
    
    nTickcount = GetTickCount
    
    End Function
    The nice thing about that code was that I could return fractions of a second. That didn't come up often but there are a few cases.

    So my fix won't have the GetTickCount problem but it only returns whole seconds (I think).

    Code:
    Public Property Get ElapsedSeconds(ByRef FromDate As Date) As Double
    
    If (FromDate = "00:00:00") Or (FromDate > Now()) Then FromDate = Now() ' FromDate is not Initialized.
    
    ElapsedSeconds = DateDiff("s", FromDate, Now())
    
    End Property
    Called as thus:

    Code:
    Private Function Random05() As Long
    Static dLastEvent As Date
    
    If ElapsedSeconds(dLastEvent) < 60 Then Exit Function
    
    ShowGamePlayNotes
    
    dLastEvent = Now()
    
    End Function

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    Re: GetTickCount - Ouch!

    Much easier way as long as you're not still supporting XP: Use GetTickCount64. Substitute Currency for ULONGLONG.

  3. #3

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: GetTickCount - Ouch!

    Well... unfortunately, I've converted most of my code now and it took days to do.

    But I've been keeping versions so I can try that in an earlier version and see how it works.

    Does it do the same thing (return a negative number) after a billion days or something?

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: GetTickCount - Ouch!

    It would be 24 days * 2 billion, i.e. around 48 billion days or around 132 million years. Most people aren't worried about their programs needing to run that long.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,014

    Re: GetTickCount - Ouch!

    Code:
    Function ElapsedSeconds(Optional ByVal FromSeconds As Currency) As Currency
        Dim CurrentTick As Currency
    
        CurrentTick = CCur(GetTickCount())
    
        If CurrentTick < FromSeconds Then
            ElapsedSeconds = (2 ^ 32) + CurrentTick - FromSeconds
        Else
            ElapsedSeconds = CurrentTick - FromSeconds
        End If
    End Function

  6. #6

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: GetTickCount - Ouch!

    I like that one. It will return fractions of seconds.

    So while researching this, I came across something that said that GetTickCount resets itself to a positive integer (long) after something like double the days it takes to crap out.

    E.g. in 49ish days it's back to normal.

    Anyone know anything about that and can shed light on it?

  7. #7
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,014

    Re: GetTickCount - Ouch!

    in VB6 GetTickCount will converts into signed LONG, 0-2147483647 and -2147483648-0 and after that reset to 0 and start over.
    unsigned LONG would be 0-4294967295 and after that reset to 0.

    the function I shared works within 49 days. so surely if the FromSeconds is above that it will not work.

  8. #8

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: GetTickCount - Ouch!

    OK, by my estimates, it will take over 100 days to complete the game if it runs non-stop.

    I'm not sure because I've never actually completed a run.

    But I'm up to 24 days.

  9. #9
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    Re: GetTickCount - Ouch!

    I'm over it...

    Days : 106
    Hours : 17
    Minutes : 20
    Seconds : 28
    Milliseconds : 495
    Ticks : 92208284950457

    32bit tick count is 634583439 (7d 8h) so yeah it got back to zero a week ago.

  10. #10

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: GetTickCount - Ouch!

    Quote Originally Posted by passel View Post
    It would be 24 days * 2 billion, i.e. around 48 billion days or around 132 million years. Most people aren't worried about their programs needing to run that long.
    True, but it's possible. So I want all my bases covered.

  11. #11
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    681

    Re: GetTickCount - Ouch!

    Write the GetTickCount64 function declaration

  12. #12
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    Re: GetTickCount - Ouch!

    Declare Function GetTickCount64 Lib "kernel32" () As Currency

    or in tB
    Declare PtrSafe Function GetTickCount64 Lib "kernel32" () As LongLong
    so you don't have to deal with stupid Currency (though of course tB supports it too)

  13. #13
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    681

    Re: GetTickCount - Ouch!

    Thank you. But a minimum of windows vista? Won't it work in Windows XP?

  14. #14
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    681

    Re: GetTickCount - Ouch!

    And how many days will the counter last? Continuously, how many days or how many years will it take before the failure?

  15. #15
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,170

    Re: GetTickCount - Ouch!

    Quote Originally Posted by HackerVlad View Post
    Thank you. But a minimum of windows vista? Won't it work in Windows XP?
    In each version of Windows, MS introduce new API's. Obviously these will only work in the versions in which they are supported. They won't work - because they are not implemented - in earlier versions. So if a particular API only works with Vista and later then it was introduced in Vista and so isn't available in previous window versions (eg XP). The converse is also sometimes true. Already implemented API's can be removed in later windows versions - so any code that uses these will only work with previous windows versions. If you need code that uses functions available only with certain versions then it's usual (at least in c code) to use compile-time conditions and run-time checks to determine which parts of the code are compiled for which windows versions. For different windows versions this could be a simple message box saying a facility is not available for that version or alternative code to provide the missing function in a different way.
    Last edited by 2kaud; Dec 12th, 2024 at 04:37 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  16. #16
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: GetTickCount - Ouch!

    I don't understand why everyone is so surprised.
    Just read the documentation:
    https://learn.microsoft.com/en-us/wi...i-gettickcount
    DWORD GetTickCount();
    Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.
    The "half" of 49.7 days IS about 24 Days, since you don't have an unsigned 32-Bit Integer in VB6

    https://learn.microsoft.com/en-us/wi...ows-data-types
    DWORD = A 32-bit unsigned integer. The range is 0 through 4294967295 decimal.

    QueryPerformanceCounter/-Frequency-Combination might be more reliable
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width