本文介绍了c#,VS2005中的定时器阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我创建了一组计时器(1-n)。起初我刚刚创建了 windows窗口定时器,但我读到系统定时器更适合 后台工作。计时器将监视不同的目录并更新数据库。没有与GUI的交互。 问题是系统计时器没有标签属性所以我可以将b $ b绑在一个对象上。 示例(旧方式): 公共类TimerInfo { public string sPath; public int iInterval; } private ArrayList arrTimers = new ArrayList(); private void CreateTimers(int NUMBER_TIMERS) { TimerInfo信息; System.Timers.Timer newTimer; for(int i = 1; i< = NUMBER_TIMERS; i ++) { Info = new TimerInfo(); Info.sPath =" c:\\" ;; Info.iInterval = 60000; newTimer = new System .Timers.Timer(); newTimer.Elapsed + = timerWatch_Tick; newTimer.Interval = 20000; newTimer.Tag = TimerInfo; newTimer.Enabled = true; arrTimers.Add(newTimer); } } private void timerWatch_Tick(object sender,EventArgs e) { string sPath =((TimerInfo)( (计时器)发送者).Tag).sPath; CheckForFile(sPath); } 如果需要,我是否需要调用Dispose重置计时器阵列? foreach(arrTimers中的System.Timers.Timer atimer) { atimer.Dispose( ); } arrTimers.Clear(); 感谢您的帮助。我正在通过C#作为新手挣扎甚至 虽然它是一种非常简洁的语言。 ~吉娜〜 解决方案 private ArrayList arrTimers = new ArrayList(); I have created an array of timers (1-n). At first I just createdwindows form timers but I read that system timers are better forbackground work. The timers will just be monitoring differentdirectories and updating a database. No interaction with the GUI.Problem is that the system timers do not have a tag property so I cantie in an object.example (old way):public class TimerInfo{public string sPath;public int iInterval;}private ArrayList arrTimers = new ArrayList();private void CreateTimers(int NUMBER_TIMERS){TimerInfo Info;System.Timers.Timer newTimer;for (int i = 1; i <= NUMBER_TIMERS; i++){Info = new TimerInfo();Info.sPath = "c:\\";Info.iInterval = 60000;newTimer = new System.Timers.Timer();newTimer.Elapsed += timerWatch_Tick;newTimer.Interval = 20000;newTimer.Tag = TimerInfo;newTimer.Enabled = true;arrTimers.Add(newTimer);}}private void timerWatch_Tick(object sender, EventArgs e){string sPath = ((TimerInfo)((Timer)sender).Tag).sPath;CheckForFile(sPath);}Do I need to call Dispose if I want to reset the timer array?foreach (System.Timers.Timer atimer in arrTimers){atimer.Dispose();}arrTimers.Clear();Thanks for your help. I am struggling as a newbie through C# eventhough it is a really neat language.~Gina~ 解决方案 这篇关于c#,VS2005中的定时器阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 08:31