how to call Change() with System.Threading.Timer

  • Thread starter Thread starter DougE
  • Start date Start date
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
 
DougE.

The first question, why are you using this one while there are two which are
easier to handle. This one in meant for multithreading.

Cor
 
Cor --

This timer exists in two ojects of the same class. These ojbjects must each
run asynchronously. At any given time during execution, either one of these
objects might wait 6000 miliseconds, 2000 miliseconds, or pause altogether.
I was hoping that using a Threading.Timer would allow for asynchronous
operations.

Thanks.

DougE
 
Doug,

What you write has in my idea nothing to do with a threading timer, probably
will the most standard windows.forms.timer do that job for you. However
never forget to set the enabling on false of those timers in the events from
those. If you dont do that, you will probably get a mesh.

O non multithreading applicaction can never be assynchrone by the way. While
by multi threading everything works work real assynchroon so you have very
few influence of the timing of that. See it as two people working on the
same project, as one want to go to the badroom, he goes.

Cor
 
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'm not understanding the purpose of the timer here. What problem are
you trying to solve by using the timer?
 
The problem is I need a timer.

Each of two objects of the same class need to run asynchronously. Each of
these two objects simply wait until the timer fires and then call procedures
in a couple of other classes including a form class. But the two objects
operate independently, that is, they call the procedures without being in
sync with the other object. One might be waiting to fire while the other is
calling, or one might wait for 5 seconds while the other waits for 2 seconds.


The objects call these procedures after the main thread wakes up from the
timer.

My whole question is this: In addition to sending variable periods for the
main thread to sleep, I want to send it -1 causing it to sleep indefinitely.
Then I want to be able to call the Change() method except I do not know how
to do that.

The documentation says you can do this. But I do not see a way to call
change from outside either the timer class or the state class.

Can I take that code out of the timeIt class, put it inside another class
along with the state class? Instead of having the timeIt class inside
another class with the state class?




Brian Gideon said:
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'm not understanding the purpose of the timer here. What problem are
you trying to solve by using the timer?
 
The problem is I need a timer.

Each of two objects of the same class need to run asynchronously. Each of
these two objects simply wait until the timer fires and then call procedures
in a couple of other classes including a form class. But the two objects
operate independently, that is, they call the procedures without being in
sync with the other object. One might be waiting to fire while the other is
calling, or one might wait for 5 seconds while the other waits for 2 seconds.

The objects call these procedures after the main thread wakes up from the
timer.

Okay, so your main thread needs to sleep a period time and then after
it wakes up it should direct two objects (of the same class) to
execute a method asynchronously right? What do you want the main
thread to be doing while these methods are executing asychronously?
My whole question is this: In addition to sending variable periods for the
main thread to sleep, I want to send it -1 causing it to sleep indefinitely.
Then I want to be able to call the Change() method except I do not know how
to do that.

You call Change like you would any other method. It's all a matter of
where you pass around that Timer reference that determines where you
can make the call.
The documentation says you can do this. But I do not see a way to call
change from outside either the timer class or the state class.

Well, the Timer class defines the Change method and you didn't code it
so you have no choice but to call it from outside the Timer class.
Because your timerState class declares the tmr variable as public
other classes can access and thus be able call methods on it.
Can I take that code out of the timeIt class, put it inside another class
along with the state class? Instead of having the timeIt class inside
another class with the state class?

Yes, you can move the code around any way you want. If I had a better
understanding of what you're want to do I could recommend a pattern.
I'm not seeing what the code you posted is suppose to be doing
especially since your asynchronous method move_Car essentially does
nothing. Your main thread (which I'm assuming is executing run_Motor)
seems to be waiting for that asynchronous method to complete in a
rather unconventional and pointless maner. Speaking of that, there's
a subtle bug there that could cause an infinite loop.
 
Well, the main thread here creates a new timeIt object. And this main thread
is within the class that spawns two asynchronous objects. So okay I need to
take the code out of timeIt class and use it as properties and methods in the
class that makes the two asynvhronous objects. So now, I have two objects
running on timers and interacting with other class/objects and a form -- in
theory. I have yet to get it working.

Would you please elaborate on the infinate loop thing?
 
[snip]

Would you please elaborate on the infinate loop thing?

This loop...

While Not (s.tmr Is Nothing)
Thread.Sleep(0)
End While

....may never stop. The reason is because you're setting s.tmr to
nothing in a thread other than the one executing this code without
using the proper synchronization primitives. The issue is regarding
when reads and writes actually occur. See the following article for
more information.

http://www.yoda.arachsys.com/csharp/threads/volatility.shtml
 
Back
Top