[英]C# Data in EventHandler
I have two functions. 我有两个功能。 One creates a timer, other does action when timer expires.
一个创建计时器,另一个在计时器到期时执行操作。 Timer function takes 1 additional argument (int).
计时器函数需要1个附加参数(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. 在
EventHandler
,我不知道要放什么。 Here is DoAction
: 这是
DoAction
:
public void DoAction(object sender, ElapsedEventArgs e, int l)
{
//some stuff here
}
How can I send int l
? 我如何发送
int l
?
您可以将lambda表达式订阅到引用局部变量的事件。
timer.Elapsed += (s, e) => { DoAction(s, e, t); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.