I am trying to pass an integer value into a ElapsedEventHandler so I can do logic based on the integer passed in. However, when the Event is raised, the value coming in is not the value I initialized it to. I may not be understanding completely how the delegate works.
class Example
{
Dictionary<int, Timer> timer;
public Example()
{
timer = new Dictionary<int, timer>();
for(int i = 0; i < 12; ++i)
{
timer.Add(i, new Timer(5000));
timer[i].Elapsed += delegate { TimerTickCustom(i); };
}
}
public void Process() // called each cycle
{
for(int i = 0; i < 12; ++i)
{
timer[i].Start();
}
}
private void TimerTickCustom(int i)
{
// The value of i coming in does not match the dictionary key.
}
}