D
DougE
Hi --
I am trying to implement a timer exactly the way it is shown in the
documentation. I am sending a variable to the constructor for the class to
manipulate the time-to-invoke parameter. This seems to work fine as below.
My question is: if I send a -1 to stall the timer from invoking, how do I
call the Change() method from outside the class? The documentation shows how
to do it from within the object, but I want to control the object from
elsewhere -- such as a class that contains the timer object.
Public Class timerState
Public tmr As Timer
End Class
Public Class timeIt
Public timePeriod As Integer
Public Sub New(ByVal x As Integer)
MyBase.New()
timePeriod = x
End Sub
Public Sub run_Motor()
Dim s As New timerState
' Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf move_Car)
' Create a timer that waits timePeriod seconds before
' invoking callback method move_Car. Wait timePeriod
' milliseconds call once and destroy itself.
Dim timer As New Timer(timerDelegate, s, timePeriod, -1)
' Keep a handle to the timer, so it can be disposed.
s.tmr = timer
' The main thread does nothing until the timer is disposed.
While Not (s.tmr Is Nothing)
Thread.Sleep(0)
End While
End Sub 'run_Motor
' The following method is called by the timer's delegate.
Shared Sub move_Car(ByVal state As [Object])
Dim s As timerState = CType(state, timerState)
s.tmr.Dispose()
s.tmr = Nothing
End Sub 'move_Car
End Class 'timeIt
I am trying to implement a timer exactly the way it is shown in the
documentation. I am sending a variable to the constructor for the class to
manipulate the time-to-invoke parameter. This seems to work fine as below.
My question is: if I send a -1 to stall the timer from invoking, how do I
call the Change() method from outside the class? The documentation shows how
to do it from within the object, but I want to control the object from
elsewhere -- such as a class that contains the timer object.
Public Class timerState
Public tmr As Timer
End Class
Public Class timeIt
Public timePeriod As Integer
Public Sub New(ByVal x As Integer)
MyBase.New()
timePeriod = x
End Sub
Public Sub run_Motor()
Dim s As New timerState
' Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf move_Car)
' Create a timer that waits timePeriod seconds before
' invoking callback method move_Car. Wait timePeriod
' milliseconds call once and destroy itself.
Dim timer As New Timer(timerDelegate, s, timePeriod, -1)
' Keep a handle to the timer, so it can be disposed.
s.tmr = timer
' The main thread does nothing until the timer is disposed.
While Not (s.tmr Is Nothing)
Thread.Sleep(0)
End While
End Sub 'run_Motor
' The following method is called by the timer's delegate.
Shared Sub move_Car(ByVal state As [Object])
Dim s As timerState = CType(state, timerState)
s.tmr.Dispose()
s.tmr = Nothing
End Sub 'move_Car
End Class 'timeIt