2013-05-01 82 views
15

關於編程Windows服務:如何停止我的Windows服務?如何以編程方式停止Windows服務

這是一個很簡單的例子代碼(C#):

// Here is my service class (MyTestService.cs). 
public class MyTestService:ServiceBase{ 

    // Constructor. 
    public MyTestService(){ 
     this.ServiceName = "My Test Service"; 
     return; 
    } 
}; 

// My application class (ApplicationClass.cs). 
public static class ApplicationClass{ 

    // Here is main Main() method. 
    public static void Main(){ 
     // 1. Creating a service instance 
     // and running it using ServiceBase. 
     MyTestService service = new MyTestService(); 
     ServiceBase.Run(service); 
     // 2. Performing a test shutdown of a service. 
     service.Stop(); 
     Environment.Exit(0); 
     return; 
    }; 
}; 

所以:我剛剛創建的「我的測試服務」,啓動並停止。但是當我查看我的Services.msc時 - 「我的測試服務」將繼續運行,並且只在單擊「停止」鏈接時停止。爲什麼? - 爲什麼service.Stop()命令什麼也不做?

ServiceController.Stop()也什麼也不做!

如何從Main()方法停止我的服務?

+1

確保您以管理員身份運行您的應用程序。 – Icemanind 2013-10-29 19:58:20

回答

10

Stop功能發送停止信號。它不會等待信號被接收和處理。

您將不得不等待Stop信號完成它的工作。你可以通過調用WaitForStatus

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

詳情參見:http://msdn.microsoft.com/nl-nl/library/system.serviceprocess.servicecontroller.waitforstatus(v=vs.71).aspx

Environment.Exit是一個討厭的一個。不要使用它!它會以很難的方式中止應用程序,而不會在finally塊中執行任何清理,而不會通過GC調用終結器方法,終止所有其他forground線程等。我可以想象,在停止信號離開應用程序之前,您的應用程序會中止。

6

我在我的項目

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); 
    } 
2

使用以下功能在你的代碼示例service.Stop()ServiceController.Stop()命令不執行任何操作,因爲當服務是因爲ServiceBase.Run(service)運行的切斷動作,他們不叫,它只能在停止返回服務。