> Dear all,
> could someone show me how one would complete the following MSDN
[quoted text clipped - 27 lines]
> 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
> could someone show me how one would complete the following MSDN
> example :
<snip>
> 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.