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 指令的處理器。

適用於

另請參閱