• Welcome to Valhalla Legends Archive.
 

[VB6] GTC Timer class not holding variables?

Started by Joe[x86], September 22, 2005, 08:38 PM

Previous topic - Next topic

Joe[x86]

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
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

l2k-Shadow

Are you sure you are calling the same instance of the class?
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

Joe[x86]

Dim GTCTimer as clsGTCTimer
Set GTCTimer = New clsGTCTimer
GTCTimer.Enabled = True
GTCTimer.Interval = 1000
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

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..
- Stealth
Author of StealthBot

Joe[x86]

It was something more like

Private GTCTimer as clsGTCTimer

Sub Main()
  ...
End Sub
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.