Reputation: 81
I have two functions. One creates a timer, other does action when timer expires. Timer function takes 1 additional argument (int). I need it to be passed to action. Here is the code to explain that:
public void CreateTimer(int t)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(DoAction(timer, ??, t));
timer.Interval = 4000;
timer.Enabled = true;
}
As you can see, there is ??
in EventHandler
, and I have no idea, what to put inside. Here is DoAction
:
public void DoAction(object sender, ElapsedEventArgs e, int l)
{
//some stuff here
}
How can I send int l
?
Upvotes: 0
Views: 49
Reputation: 22955
You can subscribe a lambda expression to the event where you refer to the local variable.
timer.Elapsed += (s, e) => { DoAction(s, e, t); };
Upvotes: 2