日期:2014-05-19  浏览次数:20957 次

想动态声明 timer 请问如何声明
for(int   i=0;i <100;i++)

------解决方案--------------------
System.Timers.Timer[] timer=new System.Timers.Timer[100];//100个
for (int i=0;i <100;i++)
{
timer[i]=new System.Timers.Timer(i);
}
------解决方案--------------------
Timer[] timers = new Timer[10];
for(int i=0;i <timers.Length;i++)
timers[i] = new Timer(1000);
------解决方案--------------------
我晕,都这样了还不能够举一反三么??
System.Timers.Timer timer1=new System.Timers.Timer(1000);
timer1.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed1);
System.Timers.Timer timer2=new System.Timers.Timer(2000);
timer2.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed2);
System.Timers.Timer timer3=new System.Timers.Timer(3000);
timer3.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed3);

private void timer_Elapsed1(object sender, System.Timers.ElapsedEventArgs e)
{
}
private void timer_Elapsed2(object sender, System.Timers.ElapsedEventArgs e)
{
}
private void timer_Elapsed3(object sender, System.Timers.ElapsedEventArgs e)
{
}

System.Timers.Timer [] timers = new System.Timers.Timer[100];

你爱定义多少就定义多少,你用for循环自然只能定义同一种类型的事件和时间间隔,除非你在for里面再自己细分处理
for(int i=0;i <100;i++)
{
timers[i] = new System.Timers.Timer();
timers[i].Interval = 1000;
timers[i].Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed);
}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
}