0

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

  • if you can code it better or
  • even if it works after 24.9 days

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
  • I don't know when it will restart, but, in 32 bit Excel, using the `Currency` data type might be able to hold values large enough for your problem. – Ron Rosenfeld Sep 08 '20 at 10:15
  • Why not query for LastBootUpTime via WM? With that time you can determe the number of overflows and with that the real TickCount. – ComputerVersteher Sep 12 '20 at 09:31

1 Answers1

0

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.