Author Topic: [VB6] GTC Timer class not holding variables?  (Read 2292 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[VB6] GTC Timer class not holding variables?
« on: September 22, 2005, 05:40:34 pm »
Yup. No matter what I do to it, m_Enabled and m_Interval just seem to like to null themselves.

Option Explicit

'Author: Joetheodd
'Purpose: A simulation of the VB.Timer control, using a class instead.
'Requires: GetTickCount declared.

Public Event Timer()    'The event fired when the timer goes off

Private m_Interval As Integer
Private m_Enabled As Boolean

Public Property Get Interval() As Integer
    Interval = m_Interval
End Property
Public Property Let Interval(I As Integer)
    m_Interval = I
End Property
Public Property Get Enabled() As Boolean
    Enabled = m_Enabled
End Property
Public Property Let Enabled(B As Boolean)
    m_Enabled = B
End Property
'These allow for m_Interval and m_Enabled to be set from outside this class.

Private Sub Class_Initialize()
    Call TimerProc
End Sub

Private Function ShouldRun() As Boolean
    'This is used to return wether or not the timer should run.
    If (m_Interval <> 0) And (m_Enabled = True) Then
        ShouldRun = True
    Else
        ShouldRun = False
    End If
    'Debug.Print "Enabled " & CStr(m_Enabled)
    'Debug.Print "Interval " & CStr(m_Interval)
End Function

Private Sub TimerProc()
    'This is used to fire the Timer() event at the right time.
    Dim GTC As Long 'This will be used to house GetTickCounts.
Start:
    If ShouldRun Then           'If the timer conditions are set to be enabled then
        Call Sleep(CLng(m_Interval))    'Sleep until interval is finished
        RaiseEvent Timer       'Fire Timer()
    Else
        Sleep 100
    End If
    DoEvents                    'DoEvents so the program doesn't freeze.
    GoTo Start                  'Begin again
End Sub

Private Sub Sleep(L As Long)
    'This will be used to "pause" a "parent" subroutine for the ammount of miliseconds passed in L.
    'This is set to be a Function so that the program will wait for it to return, instead of just "forking" it.
    Dim GTC As Long: GTC = GetTickCount 'This is used to store the current GTC.
    Dim Remain As Long: Remain = L
    Do Until Remain >= 100
        Call modDeclares.Sleep(100)
        Remain = Remain - 100
    Loop
    If Not Remain = 0 Then Call modDeclares.Sleep(Remain)
End Sub
I'd personally do as Joe suggests

You might be right about that, Joe.