Terminating a Timer Class Properly

rkaczano

Board Regular
Joined
Jul 24, 2013
Messages
141
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
When you finally get to the End Sub or your press End on the Debugg box, the objTimer will pass out of scope and the class will terminate.

I'd don't know enough about the API calls to know if they might leave footprints.
 
Upvote 0
You are not actually running a windows timer. You are only running a loop and calculating how long the loop lasted so there is no risk of crashing excel.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top