2013-05-01 7 views
15

プログラミングについてWindowsサービス:Windowsサービスを停止する方法は?だから、プログラムによってWindowsサービスを停止する方法

// 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; 
    }; 
}; 

:ここ

は非常に単純化されたサンプルコード(C#の)である私は、「私のテストサービス」を作成しました、それを開始し、停止しました。しかし、私がServices.mscを調べているとき、 "My Test Service"は実行し続けていて、 "Stop"リンクをクリックすると停止します。どうして? - なぜservice.Stop()コマンドは何もしませんか?

ServiceController.Stop()も何もしません!

Main()メソッドからサービスを停止するにはどうすればよいですか?

+1

アプリケーションを管理者として実行していることを確認してください。 – Icemanind

答えて

10

Stop -functionは停止信号を送信します。信号が受信されて処理されるまで待機しません。

停止信号が処理を完了するまで待つ必要があります。

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

詳細はを参照してください:あなたはWaitForStatusを呼び出すことによってそれを行うことができますhttp://msdn.microsoft.com/nl-nl/library/system.serviceprocess.servicecontroller.waitforstatus(v=vs.71).aspx

Environment.Exitは厄介なものです。それを使用しないでください!最後のブロックでクリーンナップを実行せずにGCでfinalizerメソッドを呼び出すことなく、他のすべてのforgroundスレッドなどを終了させることなく、アプリケーションを困難な方法で中止します。アプリケーションを終了させる前にアプリケーションが中止されることは想像できます。あなたのコード例service.Stop()ServiceController.Stop()コマンドで

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

サービスがServiceBase.Run(service)ので、実行している間、彼らが呼ばれていないので、何もしない操作をブロックしていると、それだけの停止に戻りますサービス。

関連する問題