ServiceController.ExecuteCommand(Int32) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Kör ett anpassat kommando på tjänsten.
public:
void ExecuteCommand(int command);
public void ExecuteCommand(int command);
member this.ExecuteCommand : int -> unit
Public Sub ExecuteCommand (command As Integer)
Parametrar
- command
- Int32
En programdefinierad kommandoflagga som anger vilket anpassat kommando som ska köras. Värdet måste vara mellan 128 och 256, inklusive.
Undantag
Ett fel uppstod vid åtkomst till ett system-API.
Det gick inte att hitta tjänsten.
Exempel
I följande kodexempel visas hur metoden används ServiceController.ExecuteCommand(Int32) för att köra anpassade kommandon i tjänstexemplet 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
Kommentarer
När du anropar ExecuteCommandändras inte tjänstens status. Om tjänsten startades förblir Runningstatusen . Om tjänsten stoppades förblir Stoppedstatusen , och så vidare. För att bearbeta det anpassade kommandot måste tjänsten åsidosätta OnCustomCommand metoden och tillhandahålla en hanterare för kommandot som identifieras av parametern command .