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 處理常式。

適用於

另請參閱