Windows 服务中未触发 ElapsedEventHandler

Posted

技术标签:

【中文标题】Windows 服务中未触发 ElapsedEventHandler【英文标题】:ElapsedEventHandler is not fired in Windows Service 【发布时间】:2016-05-29 14:12:24 【问题描述】:

我有一个非常简单的窗口服务,我是按照本教程开发的:http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/

但是我注意到 ElapsedEventHandler 从未像教程那样被触发:

  public partial class Scheduler : ServiceBase
    
        public Timer timer1 = null;
        public Scheduler()
        
            InitializeComponent();
        

        protected override void OnStart(string[] args)
        

            timer1 = new Timer();
            this.timer1.Interval = 30000;
            this.timer1.Elapsed+=new ElapsedEventHandler(this.timer1_Tick);
            Library.WriteErrorLog("Test Window service started");

        

        protected override void OnStop()
        
            timer1.Enabled = false;
            Library.WriteErrorLog("test window service stopped");
        

        private void timer1_Tick(object sender, ElapsedEventArgs e)
        
            Library.WriteErrorLog("Timer ticked and some job has been done successfully");
        
    

【问题讨论】:

【参考方案1】:

您必须启用计时器。有关示例,请参阅here。你提到的文章也是这样做的。

timer1.Enabled = true;

【讨论】:

感谢马丁!我错过了,我在挠头!

以上是关于Windows 服务中未触发 ElapsedEventHandler的主要内容,如果未能解决你的问题,请参考以下文章

Azure 服务总线触发队列在本地调试中未命中代码

在 jQueryMobile 中未触发 jQuery 单击事件

沙盒中未触发 Paypal webhook 事件

从 Outlook COM 加载项手动触发 Windows 错误报告

传单中未触发 oncopy 事件

XLForms 中未触发的事件