为什么在C#服务中使用Timer定时器 无法运行
为什么在c#服务程序里面 使用了 system.timers.timer控件 部署到系统里后 Tick事件无法触发?
对于初学者来说是个很大几率会遇到的问题(没错说的就是我自己):
正解是 “在服务里面是不能使用 System.Timers.Timer 的,原因是因为这个Timer是用于有UI界面的程序的。
遇到这个情况使用System.Threading.Timer 这个timer 就没这个问题了
代码如下:
System.Threading.Timer ThreadingTimer = null;
ThreadingTimer = new System.Threading.Timer(delegate (object state)
{
///TODO
///你要执行的代码区
ThreadingTimer.Stop();//停止定时器
}, null, -1, 5000);
ThreadingTimer.Start();//启动定时器