Add timers to an array of objects


B

Bobby

Dear all,
could someone show me how one would complete the following MSDN
example :

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class Timer1
Public Shared Sub Main()
Dim aTimer As New System.Timers.Timer()
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Set the Interval to 5 seconds.
aTimer.Interval = 5000
aTimer.Enabled = True

Console.WriteLine("Press 'q' to quit the sample.")
While Console.Read() <> CInt("q")
End While
End Sub

' Specify what you want to happen when the Elapsed event is
raised.
Private Shared Sub OnTimedEvent(source As Object, e As
ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

so that one could have Timers for different objects and in the
OnTimeEvent you could get the object to whom this timer belongs? Is
this possible at all?
Many Thanks.
 
Ad

Advertisements

L

Lloyd Sheen

Bobby said:
Dear all,
could someone show me how one would complete the following MSDN
example :

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class Timer1
Public Shared Sub Main()
Dim aTimer As New System.Timers.Timer()
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Set the Interval to 5 seconds.
aTimer.Interval = 5000
aTimer.Enabled = True

Console.WriteLine("Press 'q' to quit the sample.")
While Console.Read() <> CInt("q")
End While
End Sub

' Specify what you want to happen when the Elapsed event is
raised.
Private Shared Sub OnTimedEvent(source As Object, e As
ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

so that one could have Timers for different objects and in the
OnTimeEvent you could get the object to whom this timer belongs? Is
this possible at all?
Many Thanks.

Depending on what you want there are a couple of ways to do this.

If you mean by "different objects", objects which are associated with a
timer (not the timer itself) then you can create a class that inherits the
System.Timers.Timer class. Then add a property that allows you to specify
which object the timer is associated with.

If you mean the timer object itself then in the event the "source" argument
is a reference to the timer.

Hope this helps
Lloyd Sheen
 
Ad

Advertisements

P

Phill W.

Bobby said:
could someone show me how one would complete the following MSDN
example :

so that one could have Timers for different objects and in the
OnTimeEvent you could get the object to whom this timer belongs? Is
this possible at all?

So you want [lots of] /different/ objects, each with their own "Timer"
event, have them /all/ call the same OnTimedEvent routine and, in that
event, be able to find out /which/ of the objects just did so?

No problem. :)


Your object with a Timer looks like this ...

Class TimedObject
Public Event TimedEvent( sender as Object, e As ElapsedEventArgs )

Public Sub New(name as String, interval as Integer)
m_Name = name

m_Timer.Interval = interval
m_Timer.Start()

End Sub

Public ReadOnly Property Name() as String
Get
Return m_Name
End Get
End Property

Protected Overridable Sub OnTimedEvent( e as ElapsedEventArgs )
RaiseEvent TimedEvent( Me, e )
End Sub

Private m_Name as String
Private m_Timer as ...Timer

Private Sub m_Timer_Elapsed( _
sender as Object _
, e as ElapsedEventArgs _
) _
Handles m_Timer.Elapsed

Me.OnTimedEvent( e )

End Sub

End Class

Then, the class that's going to handles those events looks like this:

Class TimerTest
Sub Main()
Dim oThing1 as New TimedObject("Fred", 3000)
Addhandler oThing1.TimedEvent, AddressOf Thing_TimedEvent

Dim oThing2 as New TimedObject("Bob", 5000)
Addhandler oThing2.TimedEvent, AddressOf Thing_TimedEvent

Console.ReadLine()

End Sub

Private Sub OnTimedEvent( _
sender As Object _
, e As ElapsedEventArgs _
)
If TypeOf sender Is TimedObject Then
With DirectCast( sender, TimedObject )
Console.WriteLine(.Name)
End With
Else
Console.WriteLine(sender.GetType().ToString())
End if
End Sub

End Class

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top