by Thejan J Rajapakshe (7 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Wed 3rd January 2007
Date Added: Mon 8th February 2021
Rating: (1 Votes)
A StopWatch
API Declarations
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "CStopWatch"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' Win32 API declarations.
Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function timeGetDevCaps Lib "winmm.dll" (lpTimeCaps As TIMECAPS, ByVal uSize As Long) As Long
' API Structure definitions.
Private Type TIMECAPS
wPeriodMin As Long
wPeriodMax As Long
End Type
' Set aside storage for private member variables.
Private m_StartTime As Long
Private m_PeriodMin As Long
Private m_PeriodMax As Long
' Desired timer resolution.
Private Const MINRES As Long = 1
' ********************************************
' Initialize
' ********************************************
Private Sub Class_Initialize()
' Retrieve system timer resolution.
Dim tc As TIMECAPS
Call timeGetDevCaps(tc, Len(tc))
m_PeriodMin = tc.wPeriodMin
m_PeriodMax = tc.wPeriodMax
' Reprogram timer chip for 1ms resolution.
Call timeBeginPeriod(MINRES)
' Store starting time.
Call Me.Reset
End Sub
Private Sub Class_Terminate()
' Balance initial call, letting system know
' we no longer need higher-resolution.
Debug.Print timeEndPeriod(MINRES)