ServiceController.ExecuteCommand(Int32) メソッド

定義

サービスでカスタム コマンドを実行します。

public:
 void ExecuteCommand(int command);
public void ExecuteCommand(int command);
member this.ExecuteCommand : int -> unit
Public Sub ExecuteCommand (command As Integer)

パラメーター

command
Int32

実行するカスタム コマンドを示すアプリケーション定義のコマンド フラグ。 値は、128 から 256 までの範囲である必要があります。

例外

システム API にアクセスするときにエラーが発生しました。

サービスが見つかりませんでした。

次のコード例は、 ServiceController.ExecuteCommand(Int32) メソッドを使用して、 SimpleService サービスの例でカスタム コマンドを実行する方法を示しています。

using System;
using System.ServiceProcess;

namespace test_exec_cmnd
{
    class Program
    {
        private enum SimpleServiceCustomCommands { StopWorker = 128, RestartWorker, CheckWorker };
        static void Main(string[] args)
        {
            ServiceController myService = new ServiceController("SimpleService");
            myService.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker);
            myService.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker);
            myService.ExecuteCommand((int)SimpleServiceCustomCommands.CheckWorker);
        }
    }
}
Imports System.ServiceProcess



Class Program
    
    Private Enum SimpleServiceCustomCommands
        StopWorker = 128
        RestartWorker
        CheckWorker '
    End Enum 'SimpleServiceCustomCommands

    Shared Sub Main(ByVal args() As String) 
        Dim myService As New ServiceController("SimpleService")
        myService.ExecuteCommand(Fix(SimpleServiceCustomCommands.StopWorker))
        myService.ExecuteCommand(Fix(SimpleServiceCustomCommands.RestartWorker))
        myService.ExecuteCommand(Fix(SimpleServiceCustomCommands.CheckWorker))
    
    End Sub
End Class

注釈

ExecuteCommandを呼び出しても、サービスの状態は変わりません。 サービスが開始された場合、状態は Running。 サービスが停止した場合、状態は Stoppedのままです。 カスタム コマンドを処理するには、 OnCustomCommand メソッドをオーバーライドし、 command パラメーターで識別されるコマンドのハンドラーを指定する必要があります。

適用対象

こちらもご覧ください