I have a timer Class as noted below. Results get sent to a caption box on a form.
I am wondering if I am terminating this timer class properly - if at all.
Does there need to be a Terminate Method in the Class Module and run at the end of the Sub test()?
And what happens if I am constantly running the Sub Test() and stopping midway as I debug the code? Are multiple timers being started and not stopped? And if so, do I need code at the beginning of my module that terminates all timer classes currently running?
STANDARD MODULE
Public TimeVar As Date 'Timer
Public objTimer As CTimer 'Timer
Sub Test()
Dim PctDone As Single
Dim i as Single
Set objTimer = New CTimer 'Timer
objTimer.StartCounter
For i = 1 to 10000
PctDone = i / (10000)
UpdateProgressBar PctDone
Next
TimeVar = objTimer.TimeElapsed / (CDbl(60000 * 60) * 24)
End Sub
,*************************
Sub UpdateProgressBar(PctDone As Single)
With UserForm1
' Update the Caption property of the Frame control.
.FrameProgress.Caption = Format(PctDone, "0%")
' Widen the Label control.
.LabelProgress.Width = PctDone * _
(.FrameProgress.Width - 10)
End With
' The DoEvents allows the UserForm to update.
DoEvents
End Sub
'***************************
CLASS MODULE
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" _
(lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" _
(lpFrequency As Currency) As Long
Private m_CounterStart As Currency
Private m_CounterEnd As Currency
Private d_PerfFrequency As Double
Private Sub Class_Initialize()
Dim m_PerfFrequency As Currency
QueryPerformanceFrequency m_PerfFrequency
d_PerfFrequency = m_PerfFrequency
End Sub
Public Sub StartCounter()
QueryPerformanceCounter m_CounterStart
End Sub
Property Get TimeElapsed() As Double ' milliseconds
QueryPerformanceCounter m_CounterEnd
TimeElapsed = 1000# * (m_CounterEnd - m_CounterStart) / d_PerfFrequency
End Property
I am wondering if I am terminating this timer class properly - if at all.
Does there need to be a Terminate Method in the Class Module and run at the end of the Sub test()?
And what happens if I am constantly running the Sub Test() and stopping midway as I debug the code? Are multiple timers being started and not stopped? And if so, do I need code at the beginning of my module that terminates all timer classes currently running?
STANDARD MODULE
Public TimeVar As Date 'Timer
Public objTimer As CTimer 'Timer
Sub Test()
Dim PctDone As Single
Dim i as Single
Set objTimer = New CTimer 'Timer
objTimer.StartCounter
For i = 1 to 10000
PctDone = i / (10000)
UpdateProgressBar PctDone
Next
TimeVar = objTimer.TimeElapsed / (CDbl(60000 * 60) * 24)
End Sub
,*************************
Sub UpdateProgressBar(PctDone As Single)
With UserForm1
' Update the Caption property of the Frame control.
.FrameProgress.Caption = Format(PctDone, "0%")
' Widen the Label control.
.LabelProgress.Width = PctDone * _
(.FrameProgress.Width - 10)
End With
' The DoEvents allows the UserForm to update.
DoEvents
End Sub
'***************************
CLASS MODULE
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" _
(lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" _
(lpFrequency As Currency) As Long
Private m_CounterStart As Currency
Private m_CounterEnd As Currency
Private d_PerfFrequency As Double
Private Sub Class_Initialize()
Dim m_PerfFrequency As Currency
QueryPerformanceFrequency m_PerfFrequency
d_PerfFrequency = m_PerfFrequency
End Sub
Public Sub StartCounter()
QueryPerformanceCounter m_CounterStart
End Sub
Property Get TimeElapsed() As Double ' milliseconds
QueryPerformanceCounter m_CounterEnd
TimeElapsed = 1000# * (m_CounterEnd - m_CounterStart) / d_PerfFrequency
End Property