How to stop Windows service programmatically

前端 未结 3 593
名媛妹妹
名媛妹妹 2020-12-01 23:17

About programming Windows services: how to stop my windows service?

Here is a very simplified example code(C#):

// Here is my service class (MyTestSe         


        
相关标签:
3条回答
  • 2020-12-01 23:56

    The Stop-function sends a stop-signal. It does not wait till the signal is received and processed.

    You will have to wait till the Stop-signal has done it's work. You can do that by calling WaitForStatus:

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped);
    

    See for more info: http://msdn.microsoft.com/nl-nl/library/system.serviceprocess.servicecontroller.waitforstatus(v=vs.71).aspx

    Environment.Exit is a nasty one. DO NOT USE IT! It aborts your application the hard way, without performing any cleanup in finally blocks, without calling finalizer methods by the GC, it terminates all other forground threads, etc. I can imagine that your application is aborted before the stop-signal even left your application.

    0 讨论(0)
  • 2020-12-02 00:03

    I am using following functions in my project

        public static ServiceController GetService(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            return services.FirstOrDefault(_ => Contracts.Extensions.CompareStrings(_.ServiceName, serviceName));
        }
    
        public static bool IsServiceRunning(string serviceName)
        {
            ServiceControllerStatus status;
            uint counter = 0;
            do
            {
                ServiceController service = GetService(serviceName);
                if (service == null)
                {
                    return false;
                }
    
                Thread.Sleep(100);
                status = service.Status;
            } while (!(status == ServiceControllerStatus.Stopped ||
                       status == ServiceControllerStatus.Running) &&
                     (++counter < 30));
            return status == ServiceControllerStatus.Running;
        }
    
        public static bool IsServiceInstalled(string serviceName)
        {
            return GetService(serviceName) != null;
        }
    
        public static void StartService(string serviceName)
        {
            ServiceController controller = GetService(serviceName);
            if (controller == null)
            {
                return;
            }
    
            controller.Start();
            controller.WaitForStatus(ServiceControllerStatus.Running);
        }
    
        public static void StopService(string serviceName)
        {
            ServiceController controller = GetService(serviceName);
            if (controller == null)
            {
                return;
            }
    
            controller.Stop();
            controller.WaitForStatus(ServiceControllerStatus.Stopped);
        }
    
    0 讨论(0)
  • 2020-12-02 00:12

    In your code example service.Stop() and ServiceController.Stop() commands does nothing because they are not called while service is running since ServiceBase.Run(service) is blocking operation and it returns only on stop of the service.

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