Manfred
Manfred

Reputation: 1

Timer overflow after 25 days by long-variable in VBA, ring counter technique

I am programming some winsocket-applications with vba and there is the possibity of time outs.

I have experimented with API-Timer Settimer and Killtimer, but there you have to be careful to start and stop the timer parallel to the normal vba program.

The included timer() function in vba is not very precise, so i decided to program the timer with GetTickCount on a 32 bit office system.

But here is the problem that after 24.9 days the Counter restarts. On newer systems you could use longptr and GetTickCount64 i now, but it should run on an old Windows 32 Bit System as well.

So i coded something like a ring counter for about 1 sec, but i am not sure

So could you give some response to my test program (coded in excel vba):

Option Explicit
Declare Function GetTickCount Lib "kernel32" () As Long

Sub sTest()
Dim intStart As Integer
Dim intDelta As Integer
Do
    intStart = fxTimer1024
    intDelta = fxTimerDelta(intStart)
    Do While intDelta < 200
        DoEvents 'endless loop, use ctrl+pause/break to end
        intDelta = fxTimerDelta(intStart)
    Loop
    Debug.Print intStart, intDelta
Loop
End Sub

Function fxTimer1024()
fxTimer1024 = GetTickCount() Mod 1024
End Function

Function fxTimerDelta(intStart)
Dim x As Long
x = fxTimer1024
If intStart > x Then
   fxTimerDelta = x + 1024 - intStart
Else
   fxTimerDelta = x - intStart
End If
End Function

Upvotes: 0

Views: 151

Answers (1)

Manfred
Manfred

Reputation: 1

Thanks, currency is a goot tip, especially for other problems, here is the problem that the return value of GetTicketCount is only 4 byte-integer and only counts to this value, so there is no effect to assign it to a currency variable.

Upvotes: 0

Related Questions