ServiceController.ExecuteCommand(Int32) Méthode

Définition

Exécute une commande personnalisée sur le service.

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

Paramètres

command
Int32

Indicateur de commande défini par l'application qui indique la commande personnalisée à exécuter. La valeur doit être comprise entre 128 et 256, inclus.

Exceptions

Une erreur s'est produite lors de l'accès à une API système.

Le service est introuvable.

Exemples

L’exemple de code suivant montre l’utilisation de la ServiceController.ExecuteCommand(Int32) méthode pour exécuter des commandes personnalisées dans l’exemple de SimpleService service.

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

Remarques

Lorsque vous appelez ExecuteCommand, l’état du service ne change pas. Si le service a été démarré, l’état reste Running. Si le service a été arrêté, l’état reste Stopped, et ainsi de suite. Pour traiter la commande personnalisée, le service doit remplacer la OnCustomCommand méthode et fournir un gestionnaire pour la commande identifiée par le command paramètre.

S’applique à

Voir aussi