Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

Add timers to an array of objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bobby - 31 Oct 2007 15:37 GMT
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.
Lloyd Sheen - 31 Oct 2007 16:25 GMT
> 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
Phill W. - 31 Oct 2007 18:20 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.