为什么在C#服务中使用Timer定时器 无法运行

1103
补充展位 Pages_Weblog_Get#0
文章摘要
此内容由人工摘要内容,并由AI根据文章内容进行润色
暂无内容

为什么在c#服务程序里面 使用了 system.timers.timer控件 部署到系统里后 Tick事件无法触发?

为什么在C#服务中使用Timer定时器 无法运行 - Yuan天空

对于初学者来说是个很大几率会遇到的问题(没错说的就是我自己):

正解是 “在服务里面是不能使用 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();//启动定时器
补充展位
Pages_Weblog_Get#9cfb96e4-8f10-4373-8072-1a7f1d40f976
补充展位 Pages_Weblog_Get#1
补充展位 Pages_Weblog_Get#2
专题推荐
暂无内容
补充展位 Pages_Weblog_Get#3