Jump to content

C# Timer List

Nater

I have a list of Timers (System.Timers) all connected to the same elapse, but how do I figure out which one is communicating with it? Each one is suppose to be active at the same time, all for different amounts of times and I need to know what time each one is at at all times.

 

The only thing I've come up with is doing it based on intervals ie: ones at 999, another at 998, another at 1000 ect ect. 

Link to comment
Share on other sites

Link to post
Share on other sites

Just FYI, you can simplify the code with Contains()

// Using List<Timer> which I'll simply name _timersprivate void timer1_Tick(...){    Timer t = (Timer) sender    if (_timers.Contains(t))    {        // Do something    }}

However you may want to consider using a Dictionary or HashSet instead of a List for faster lookup.

// If using HashSet<Timer> which I'll simply name _timersprivate void timer1_Tick(...){    Timer t = (Timer) sender    if (_timers.Contains(t))    {        // Do something    }}// If using Dictionary<Timer, ...> which I'll also name _timersprivate void timer1_Tick(...){    Timer t = (Timer) sender    if (_timers.ContainsKey(t))    {        // Do something    }}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×