ServiceController.WaitForStatus Método

Definición

Espera a que el servicio alcance el estado especificado.

Sobrecargas

Nombre Description
WaitForStatus(ServiceControllerStatus)

Espera infinitamente que el servicio alcance el estado especificado.

WaitForStatus(ServiceControllerStatus, TimeSpan)

Espera a que el servicio llegue al estado especificado o para que expire el tiempo de espera especificado.

WaitForStatus(ServiceControllerStatus)

Espera infinitamente que el servicio alcance el estado especificado.

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)

Parámetros

desiredStatus
ServiceControllerStatus

Estado que se va a esperar.

Excepciones

El desiredStatus parámetro no es ninguno de los valores definidos en la ServiceControllerStatus enumeración.

Ejemplos

En el ejemplo siguiente se usa la ServiceController clase para comprobar si el servicio Alerter está detenido. Si el servicio se detiene, el ejemplo inicia el servicio y espera hasta que el estado del servicio esté establecido en 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

Comentarios

Use WaitForStatus para suspender el procesamiento de una aplicación hasta que el servicio haya alcanzado el estado necesario.

Note

El WaitForStatus método espera aproximadamente 250 milisegundos entre cada comprobación de estado. WaitForStatus no puede detectar el caso del servicio observado cambiando a desiredStatus y, a continuación, inmediatamente a otro estado en ese intervalo.

Consulte también

Se aplica a

WaitForStatus(ServiceControllerStatus, TimeSpan)

Espera a que el servicio llegue al estado especificado o para que expire el tiempo de espera especificado.

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)

Parámetros

desiredStatus
ServiceControllerStatus

Estado que se va a esperar.

timeout
TimeSpan

Objeto TimeSpan que especifica la cantidad de tiempo que se va a esperar a que el servicio alcance el estado especificado.

Excepciones

El desiredStatus parámetro no es ninguno de los valores definidos en la ServiceControllerStatus enumeración.

El valor especificado para el timeout parámetro expira.

Comentarios

Use WaitForStatus para suspender el procesamiento de una aplicación hasta que el servicio haya alcanzado el estado necesario.

Note

El WaitForStatus método espera aproximadamente 250 milisegundos entre cada comprobación de estado. WaitForStatus no puede detectar el caso del servicio observado cambiando a desiredStatus y, a continuación, inmediatamente a otro estado en ese intervalo.

Consulte también

Se aplica a