Valhalla Legends Forums Archive | Visual Basic Programming | [VB6] GTC Timer class not holding variables?

AuthorMessageTime
JoeTheOdd
Yup. No matter what I do to it, m_Enabled and m_Interval just seem to like to null themselves.

[code]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[/code]
September 23, 2005, 1:38 AM
l2k-Shadow
Are you sure you are calling the same instance of the class?
September 23, 2005, 3:11 AM
JoeTheOdd
[code]Dim GTCTimer as clsGTCTimer
Set GTCTimer = New clsGTCTimer
GTCTimer.Enabled = True
GTCTimer.Interval = 1000[/code]
September 23, 2005, 2:46 PM
Stealth
If that code you posted is running inside of a method, then the object you created with the Dim statement will fall out of scope when that method has finished running, and all your values will return to 0..
September 23, 2005, 5:54 PM
JoeTheOdd
It was something more like

[code]Private GTCTimer as clsGTCTimer

Sub Main()
  ...
End Sub[/code]
September 23, 2005, 11:57 PM

Search