System.Timers.Timer timer1_Elapsed not firing! Help!

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

提交回复
热议问题