by John Lambert (2 Submissions)
Category: Debugging and Error Handling
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (4 Votes)
This code keeps a count in milliseconds of how long it takes between calls of StartTimer and StopTimer or Elapsed.
Inputs
None.
Assumes
Make a class called clsTimer and paste this in there.
Call order is something like:
Dim t1 as new clsTimer
t1.StartTimer
'do something that takes a while
Debug.Print "Right now, current elapsed = " & t2.Elapsed
'do something else?
t2.StopTimer
Debug.Print "Total elapsed = " & t2.Elapsed
Code Returns
.Elapsed returns the number of milliseconds between calls to .StartTimer and .StopTimer. If .StopTimer hasn't been called, it returns the number of milliseconds since .StartTimer was called.
Side Effects
No side effects.
API DeclarationsPrivate Declare Function GetTickCount Lib "kernel32" () As Long
' in clsTimer...
Dim start, finish
Public Sub StopTimer()
finish = GetTickCount()
End Sub
Public Sub StartTimer()
start = GetTickCount()
finish = 0
End Sub
Public Sub DebugTrace(v)
Debug.Print v & " " & Elapsed()
End Sub
Public Property Get Elapsed()
If finish = 0 Then
Elapsed = GetTickCount() - start
Else
Elapsed = finish - start
End If
End Property