Easier way to debug a Windows service

后端 未结 28 1927
春和景丽
春和景丽 2020-11-22 15:36

Is there an easier way to step through the code than to start the service through the Windows Service Control Manager and then attaching the debugger to the thread? It\'s ki

28条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 16:11


    static void Main()
    {
    #if DEBUG
                    // Run as interactive exe in debug mode to allow easy
                    // debugging.
    
                    var service = new MyService();
                    service.OnStart(null);
    
                    // Sleep the main thread indefinitely while the service code
                    // runs in .OnStart
    
                    Thread.Sleep(Timeout.Infinite);
    #else
                    // Run normally as service in release mode.
    
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[]{ new MyService() };
                    ServiceBase.Run(ServicesToRun);
    #endif
    }
    

提交回复
热议问题