I got several devices which send a TCP or POST message each minute. I need to alarm whenever a device stop sending messages after one hour.
I thought about creating one Timer for each device and restarting it whenever a message is received. If the event triggers, call a method with the corresponding ID of the device associated to the timer and generate the alert.
The problem is that I can't find a way to pass fixed parameters to the Timers. Every method I tried somehow the chosen parameter gets replaced the same for every timer.
Is there a better approach? Or which is the correct way to pass parameters to the elapsed event of the timer?
Thanks,
Edit:
This code prints always 5, I need it to print 1 2 3 4 5 (doesn't matter the order)
private Timer[] timers = new Timer[5];
private void button6_Click(object sendera, EventArgs ee)
{
for (int i = 0; i < timers.Length; i++)
{
timers[i] = new Timer(1000);
timers[i].Elapsed += (sender, e) => { HandleTimerElapsed(i); };
timers[i].Start();
}
}
static void HandleTimerElapsed(int i)
{
Console.WriteLine(i);
}