日期:2014-05-17  浏览次数:20891 次

C#中关于System.Timers.Timer 的问题
本帖最后由 wait_876 于 2013-10-10 21:32:30 编辑
在一个窗体中需要用到较多的Timer,在程序开始时,事先声明了若干个Timer,假设先声明5个,其实可能更多个
public static System.Timers.Timer Timer1;
public static System.Timers.Timer Timer2;
public static System.Timers.Timer Timer3;
public static System.Timers.Timer Timer4;
public static System.Timers.Timer Timer5;
然后希望在运行之后,根据用户的操作来对特定的Timer进行操作,本人较菜现在用的是switch,需要一一枚举
不过要是Timer一多,就感觉很麻烦,有没有一种方法,比如说我现在知道“Timer1”(字符串)了,然后得到Timer1对象呢?
另外请教下有没有方便的方法生成多个有规律名字的Timer对象呢?比如Timer1,Timer2..Timer10..这样?
求指导~
c# Timer

------解决方案--------------------
public static System.Timers.Timer[] timers = new System.Timers.Timer[] { timer1, timer2, timer3, timer4, timer5 };

用timers[n - 1] 访问。

------解决方案--------------------
用数组最简单了,也可以用字典啊:
Dictionary<string, System.Timers.Timer> dic = new Dictionary<string, System.Timers.Timer>();
dic["timer1"] = new System.Timers.Timer();