namespace mynamespace { public delegate void methodDelegate(object o); public class TimerHandler { private static methodDelegate _method; private static methodDelegate method { get { return _method; } } private static Timer _timer = null; private static Timer timer { get { if (_timer != null) { return _timer; } else { return new Timer(new TimerCallback(method), null, Timeout.Infinite, Timeout.Infinite); } } } ////// 启动计时器 /// public void Start(methodDelegate method) { _method = method; int ScanInterval = 6 * 1000;//ms,6s默认时间 if (System.Configuration.ConfigurationSettings.AppSettings["ScanInterval"] != null) { ScanInterval = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["ScanInterval"]) * 1000; } timer.Change(0, ScanInterval); } ////// 停止计时器 /// public void Stop() { timer.Change(Timeout.Infinite, Timeout.Infinite); } }}
使用:
TimerHandler t = new TimerHandler(); t.Start(methodName);