473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Add timers to an array of objects

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.

Oct 31 '07 #1
2 1449

"Bobby" <ro****************@cern.chwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
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

Oct 31 '07 #2
Bobby wrote:
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.
Oct 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: GDumencu | last post by:
I have a C# program that have to run all the time. For some reasons I cannot make it a service. This program has 3 timers and by time to time ( days or weeks) one of them stops. Some times, starts...
1
by: Antoine | last post by:
Hello, Does anybody know a way to retreive the running timers (their ids) in a html page? I cannot find something in the html dom at first sight. Is there collection of timers available (like...
1
by: Bamse | last post by:
Hi, can timers be used in webservices? as an example: to check at some time interval an object - Application and for each logged user, check its login period, and if it is greater than 30...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
1
by: logdenav | last post by:
Hello I'm testing the performance of the System.Timers.Timer class. I created a small program that create 100 User objects. Each USer object create a MyTimer object. The constructor of the User...
4
by: TheJediMessiah | last post by:
Hi, I have a number of objects for which I want to create a timer for. So for each object I would like to have a timer which runs at a different interval. For a single timer I know how to...
1
by: Jonathan Woods | last post by:
Hi there, I have three methods these need to execute at every interval time. I would like to know which option is better? Option A) Three System.Timers.Timer objects these execute each...
5
by: FredC | last post by:
I have an application that uses mulitple timers. Each of the timer event handlers manipulate a common array of data. I'm getting Null refererance errors - should I put a lock on the array when I...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.