Easier way to debug a Windows service

后端 未结 28 1993
春和景丽
春和景丽 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:27

    static class Program
    {
        static void Main()
        {
            #if DEBUG
    
            // TODO: Add code to start application here
    
            //    //If the mode is in debugging
            //    //create a new service instance
            Service1 myService = new Service1();
    
            //    //call the start method - this will start the Timer.
            myService.Start();
    
            //    //Set the Thread to sleep
            Thread.Sleep(300000);
    
            //    //Call the Stop method-this will stop the Timer.
            myService.Stop();
    
             #else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1() 
            };
    
            ServiceBase.Run(ServicesToRun);
             #endif
        }
    }
    

提交回复
热议问题