ServiceController.WaitForStatus Metodo

Definizione

Attende che il servizio raggiunga lo stato specificato.

Overload

Nome Descrizione
WaitForStatus(ServiceControllerStatus)

Attende infinitamente che il servizio raggiunga lo stato specificato.

WaitForStatus(ServiceControllerStatus, TimeSpan)

Attende che il servizio raggiunga lo stato specificato o che scada il timeout specificato.

WaitForStatus(ServiceControllerStatus)

Attende infinitamente che il servizio raggiunga lo stato specificato.

public:
 void WaitForStatus(System::ServiceProcess::ServiceControllerStatus desiredStatus);
public void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desiredStatus);
member this.WaitForStatus : System.ServiceProcess.ServiceControllerStatus -> unit
Public Sub WaitForStatus (desiredStatus As ServiceControllerStatus)

Parametri

desiredStatus
ServiceControllerStatus

Stato di attesa.

Eccezioni

Il desiredStatus parametro non è uno dei valori definiti nell'enumerazione ServiceControllerStatus .

Esempio

Nell'esempio seguente viene utilizzata la ServiceController classe per verificare se il servizio Alerter è stato arrestato. Se il servizio viene arrestato, l'esempio avvia il servizio e attende che lo stato del servizio sia impostato su Running.

// Check whether the Alerter service is started.
ServiceController^ sc = gcnew ServiceController;
if ( sc )
{
   sc->ServiceName =  "Alerter";
   Console::WriteLine(  "The Alerter service status is currently set to {0}", sc->Status );
   if ( sc->Status == (ServiceControllerStatus::Stopped) )
   {
      // Start the service if the current status is stopped.
      Console::WriteLine(  "Starting the Alerter service..." );
      try
      {
         // Start the service, and wait until its status is "Running".
         sc->Start();
         sc->WaitForStatus( ServiceControllerStatus::Running );
         
         // Display the current service status.
         Console::WriteLine(  "The Alerter service status is now set to {0}.", sc->Status );
      }
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine(  "Could not start the Alerter service." );
      }
   }
}

// Check whether the Alerter service is started.

ServiceController sc  = new ServiceController();
sc.ServiceName = "Alerter";
Console.WriteLine("The Alerter service status is currently set to {0}",
                   sc.Status);

if (sc.Status == ServiceControllerStatus.Stopped)
{
   // Start the service if the current status is stopped.

   Console.WriteLine("Starting the Alerter service...");
   try
   {
      // Start the service, and wait until its status is "Running".
      sc.Start();
      sc.WaitForStatus(ServiceControllerStatus.Running);

      // Display the current service status.
      Console.WriteLine("The Alerter service status is now set to {0}.",
                         sc.Status);
   }
   catch (InvalidOperationException)
   {
      Console.WriteLine("Could not start the Alerter service.");
   }
}

' Check whether the Alerter service is started.

Dim sc As New ServiceController()
sc.ServiceName = "Alerter"
Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status)

If sc.Status = ServiceControllerStatus.Stopped Then
   ' Start the service if the current status is stopped.
   Console.WriteLine("Starting the Alerter service...")

   Try
      ' Start the service, and wait until its status is "Running".
      sc.Start()
      sc.WaitForStatus(ServiceControllerStatus.Running)
      
      ' Display the current service status.
      Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status)
   Catch 
      Console.WriteLine("Could not start the Alerter service.")
   End Try
End If

Commenti

Usare WaitForStatus per sospendere l'elaborazione di un'applicazione fino a quando il servizio non ha raggiunto lo stato richiesto.

Note

Il WaitForStatus metodo attende circa 250 millisecondi tra ogni controllo di stato. WaitForStatus non è in grado di rilevare il caso del servizio osservato che passa a desiredStatus e quindi immediatamente a un altro stato in tale intervallo.

Vedi anche

Si applica a

WaitForStatus(ServiceControllerStatus, TimeSpan)

Attende che il servizio raggiunga lo stato specificato o che scada il timeout specificato.

public:
 void WaitForStatus(System::ServiceProcess::ServiceControllerStatus desiredStatus, TimeSpan timeout);
public void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desiredStatus, TimeSpan timeout);
member this.WaitForStatus : System.ServiceProcess.ServiceControllerStatus * TimeSpan -> unit
Public Sub WaitForStatus (desiredStatus As ServiceControllerStatus, timeout As TimeSpan)

Parametri

desiredStatus
ServiceControllerStatus

Stato di attesa.

timeout
TimeSpan

Oggetto TimeSpan che specifica la quantità di tempo di attesa che il servizio raggiunga lo stato specificato.

Eccezioni

Il desiredStatus parametro non è uno dei valori definiti nell'enumerazione ServiceControllerStatus .

Il valore specificato per il timeout parametro scade.

Commenti

Usare WaitForStatus per sospendere l'elaborazione di un'applicazione fino a quando il servizio non ha raggiunto lo stato richiesto.

Note

Il WaitForStatus metodo attende circa 250 millisecondi tra ogni controllo di stato. WaitForStatus non è in grado di rilevare il caso del servizio osservato che passa a desiredStatus e quindi immediatamente a un altro stato in tale intervallo.

Vedi anche

Si applica a