使用Timer做定时器时注意设置为私有,以防被垃圾回收器回收
至于使用System.Timers.Timer 还是System.Threading.Timer 还未具体测试,如果以后要用,有什么不同看下MSDN
private System.Timers.Timer Timer4InitState = null;
Timer4InitState = new System.Timers.Timer(); Timer4InitState.Interval = 1000 * 60 * Common.UpdateInitStateInterval; Timer4InitState.Elapsed += new System.Timers.ElapsedEventHandler(DoWork); Timer4InitState.Enabled = false; Timer4InitState.Start();
protected override void OnStart(string[] args) { Timer4InitState.Enabled = true; RegisterLog4net(); log.Info("服务启动....");
DoWork(null,null)
}
原文地址:http://www.cnblogs.com/Rannie/p/3922614.html