C# system.timer.time 传递参数
privatestaticSystem.Timers.TimeraTimer;publicstaticvoidMain(){inta;aTimer=newSystem.T...
private static System.Timers.Timer aTimer;
public static void Main()
{
int a;
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
OnTimedEvent()想调用main()里面的一个变量a ,可以有什么办法吗?
谢谢了!!! 展开
public static void Main()
{
int a;
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
OnTimedEvent()想调用main()里面的一个变量a ,可以有什么办法吗?
谢谢了!!! 展开
2个回答
展开全部
private static System.Timers.Timer aTimer;
public static void Main()
{
int a = 12345;
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler((s, e) => OnTimedEvent(s, e, a));
aTimer.Interval = 2000;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e, int a)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
添加事件订阅时,采用了一个匿名方法传入a
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询