转自:http://bbs.csdn.net/topics/390889626
问:System.Timers.Timer怎么给刷新函数传递多个参数?不使用全局变量
解决:如果方便传递参数,可以自己继承timer,写一个自己的timer类即可
例:
class MyTimer : System.Timers.Timer
{
int num;
public int GetNum()
{
return num;
}
public void SetNum(int value)
{
num = value;
}
}
void taskTime_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MyTimer t = (MyTimer)sender;
t.Enabled = false;
t.Close();
}
private void button1_Click(object sender, EventArgs e)
{
//string id = "System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
//var y = Assembly.Load(id);
for (int i = 1; i <= 3; i++)
{
MyTimer taskTime = new MyTimer();
//我这里想把i传到taskTime_Elapsed里 然后方法里使用!因为是多个,全局参数不能用
taskTime.Interval = i * 1000;
taskTime.SetNum(i);
taskTime.Enabled = true;
taskTime.AutoReset = true;
taskTime.Elapsed += new System.Timers.ElapsedEventHandler(taskTime_Elapsed);
}
}