ElapsedEventHandler 不会在 Windows Service 中触发



我有非常简单的窗口服务,这是我在遵循本教程后开发的: 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");
        }
    }

您必须启用计时器。有关示例,请参阅此处。您引用的文章也这样做。

timer1.Enabled = true;

相关内容

  • 没有找到相关文章

最新更新