ServiceController.WaitForStatus Metoda

Definicja

Czeka na osiągnięcie określonego stanu przez usługę.

Przeciążenia

WaitForStatus(ServiceControllerStatus)

Nieskończenie czeka na osiągnięcie określonego stanu usługi.

WaitForStatus(ServiceControllerStatus, TimeSpan)

Czeka na osiągnięcie określonego stanu usługi lub wygaśnięcie określonego limitu czasu.

WaitForStatus(ServiceControllerStatus)

Nieskończenie czeka na osiągnięcie określonego stanu usługi.

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)

Parametry

desiredStatus
ServiceControllerStatus

Stan oczekiwania.

Wyjątki

Parametr desiredStatus nie jest żadnym z wartości zdefiniowanych w wyliczeniem ServiceControllerStatus .

Przykłady

W poniższym przykładzie użyto ServiceController klasy , aby sprawdzić, czy usługa Alerter jest zatrzymana. Jeśli usługa zostanie zatrzymana, przykład uruchamia usługę i czeka na ustawienie stanu usługi na Runningwartość .

// 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.ToString());

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.ToString());
   }
   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

Uwagi

Użyj polecenia WaitForStatus , aby wstrzymać przetwarzanie aplikacji do momentu osiągnięcia wymaganego stanu usługi.

Uwaga

Metoda WaitForStatus czeka około 250 milisekund między każdym sprawdzeniem stanu. WaitForStatus Program nie może wykryć przypadku zmiany obserwowanej usługi na desiredStatus , a następnie natychmiast na inny stan w tym interwale.

Zobacz też

Dotyczy

WaitForStatus(ServiceControllerStatus, TimeSpan)

Czeka na osiągnięcie określonego stanu usługi lub wygaśnięcie określonego limitu czasu.

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)

Parametry

desiredStatus
ServiceControllerStatus

Stan oczekiwania.

timeout
TimeSpan

TimeSpan Obiekt określający czas oczekiwania na osiągnięcie określonego stanu usługi.

Wyjątki

Parametr desiredStatus nie jest żadnym z wartości zdefiniowanych w wyliczeniem ServiceControllerStatus .

Wartość określona dla parametru timeout wygasa.

Uwagi

Użyj polecenia WaitForStatus , aby wstrzymać przetwarzanie aplikacji do momentu osiągnięcia wymaganego stanu usługi.

Uwaga

Metoda WaitForStatus czeka około 250 milisekund między każdym sprawdzeniem stanu. WaitForStatus Program nie może wykryć przypadku zmiany obserwowanej usługi na desiredStatus , a następnie natychmiast na inny stan w tym interwale.

Zobacz też

Dotyczy