Home All Groups Group Topic Archive Search About

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

Author
29 Nov 2007 2:20 AM
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

Author
29 Nov 2007 6:05 AM
Cor Ligthert[MVP]
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
Author
29 Nov 2007 5:38 PM
DougE
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

Show quoteHide quote
"Cor Ligthert[MVP]" wrote:

> 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
>
Author
29 Nov 2007 5:52 PM
Cor Ligthert[MVP]
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

Show quoteHide quote
"DougE" <Do***@discussions.microsoft.com> schreef in bericht
news:7C7D97E8-24F4-42F5-A3FE-3CE129813D11@microsoft.com...
> 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
>
> "Cor Ligthert[MVP]" wrote:
>
>> 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
>>
Author
29 Nov 2007 8:34 PM
Brian Gideon
On Nov 28, 8:20 pm, DougE <Do***@discussions.microsoft.com> wrote:
Show quoteHide quote
> 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?