System.Timers.Timer timer1_Elapsed not firing! Help!

前端 未结 4 670
陌清茗
陌清茗 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.

    0 讨论(0)
  • 2021-01-22 22:56

    System.Timers.Timer.Start() has a same function as System.Timers.Timer.Enabled=true; System.Timers.Timer.Stop() has a same function as System.Timers.Timer.Enabled=false;

    Those 2 methods internally sets Enabled property to true or false which starts or stops the timer.

    Check if you have permission to write to eventlog. You can also check for errors in eventlog.

    System.Timers.Timer is not thread safe. Make sure you use it properly.

    0 讨论(0)
  • 2021-01-22 22:59

    It looks like it works but you probably a better way to debug it. Who knows maybe your eventLog1 is null

    Update your onstart to include this

     protected override void OnStart(string[] args)
         {
            foreach (string arg in args)
            {
                if (arg == "DEBUG_SERVICE")
                        DebugMode();
    
            }
    
         #if DEBUG
             DebugMode();
         #endif
    
         eventLog1.WriteEntry("Service Started");
         timer1.Elapsed += timer1_Elapsed;
         timer1.Interval = 10000;
         timer1.Enabled = true;
    
        }
    
    private static void DebugMode()
    {
    
        Debugger.Break();
    }
    

    Now when you hit start on the service you'll get a dialog asking if you want to attach. Its way easier then trying to attach manually.

    0 讨论(0)
  • 2021-01-22 23:16

    I think timer1_Elapsed needs to use the event delegate, so

    timer1_Elapsed +=  new ElapsedEventHandler(timer1_Elapsed);
    

    http://msdn.microsoft.com/es-es/library/system.timers.timer.aspx

    Though I would recommend the System.Threading.Timer for inside a service, as it will correctly throw exceptions inside the elapsed event. System.Timers.Timer just swallows them.

    http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

    0 讨论(0)
提交回复
热议问题