471,318 Members | 3,236 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

creating an array of System.Windows.Forms.Timers

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 implement

timer = new System.Windows.Forms.Timer();
timer.Tick += new EventHandler(Timer_Tick);
timer.Interval = 1000 * obj.InterleavedMessage.Time;
timer.Enabled = true;

But how can I have an array of timers for an array of objects which
will have different intervals?
For each timer I would like to call a different timer_tick handler.

Cheers

Feb 1 '06 #1
4 7609
TheJediMessiah wrote:
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 implement

timer = new System.Windows.Forms.Timer();
timer.Tick += new EventHandler(Timer_Tick);
timer.Interval = 1000 * obj.InterleavedMessage.Time;
timer.Enabled = true;

But how can I have an array of timers for an array of objects which
will have different intervals?
For each timer I would like to call a different timer_tick handler.


Just do it the same way you would with any other array:

Timer[] timers = new Timer[someSize];

timers[0] = new Timer();
timers[0].Tick += ...;

etc

Jon

Feb 1 '06 #2
You have not filled in the most critical part but just left it with ...

If I create an array of timers like you have suggested.

Timer[] timers = new Timer[someSize];
for(int i=0;i<someSize;i++)
{
timer[i].Tick += new EventHandler(Timer_Tick);
timer[i].Interval = 1000 * obj[i].InterleavedMessage.Time;
timer[i].Enabled = true;
}

Is this correct?
How do I know which Timer_Tick which timer object has raised it???

Feb 2 '06 #3
TheJediMessiah wrote:
You have not filled in the most critical part but just left it with ...
Well yes - I was assuming that having filled in a couple of properties,
the way to do the rest was obvious.
If I create an array of timers like you have suggested.

Timer[] timers = new Timer[someSize];
for(int i=0;i<someSize;i++)
{
timer[i].Tick += new EventHandler(Timer_Tick);
timer[i].Interval = 1000 * obj[i].InterleavedMessage.Time;
timer[i].Enabled = true;
}

Is this correct?
Yes.
How do I know which Timer_Tick which timer object has raised it???


The first parameter to the event is the "source" which should be the
Timer generating the event.

Jon

Feb 2 '06 #4
Thanks.
I created subclass of the Timer class and added an ID property so I
knew which timer was passed into TImer_Tick.

Thanks again for your help.

Feb 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Mark Rae | last post: by
5 posts views Thread by Michael C# | last post: by
1 post views Thread by melanieab | last post: by
4 posts views Thread by Liverpool fan | last post: by
3 posts views Thread by hazz | last post: by
reply views Thread by rosydwin | last post: by

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.