System.Timers.Timer timer1_Elapsed not firing! Help!

前端 未结 4 710
陌清茗
陌清茗 2021-01-22 22:14

I am creating another windows service and my timer is not ticking and I have no idea why! I am using system.timers.timer as I have in previous services and it doesn\'t work. I

4条回答
  •  灰色年华
    2021-01-22 22:53

    System.Timers.Timer is an ugly timer. One nasty thing it does is swallow exceptions raised by the Elapsed event handler. Which will kill your timer since you stop it when entering the method. No notification whatsoever, it just stops working.

    You have to at least add exception handling to this code so you can log the exception and stop the service.

    Also beware the bug in your OnStart() method, you'll keep adding an event handler each time the service gets started. The Elapsed event runs multiple times, in itself a good way to bomb something.

    Consider System.Threading.Timer, it doesn't have any of these problems.

提交回复
热议问题