ServiceController.WaitForStatus 方法

定義

等候服務到達指定的狀態。

多載

WaitForStatus(ServiceControllerStatus)

無限期等候服務到達指定的狀態。

WaitForStatus(ServiceControllerStatus, TimeSpan)

等候服務到達指定的狀態或指定的逾時到期。

WaitForStatus(ServiceControllerStatus)

來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs

無限期等候服務到達指定的狀態。

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)

參數

desiredStatus
ServiceControllerStatus

要等候的狀態。

例外狀況

desiredStatus 參數不是 ServiceControllerStatus 列舉中定義的任何一個值。

範例

下列範例會 ServiceController 使用 類別來檢查 Alerter 服務是否已停止。 如果服務已停止,此範例會啟動服務,並等到服務狀態設定為 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

備註

使用 WaitForStatus 暫停應用程式的處理,直到服務達到必要的狀態為止。

注意

方法 WaitForStatus 會在每個狀態檢查之間等候大約 250 毫秒。 WaitForStatus 無法偵測觀察到的服務變更為 desiredStatus 的情況,然後立即在該間隔中另一個狀態。

另請參閱

適用於

WaitForStatus(ServiceControllerStatus, TimeSpan)

來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs

等候服務到達指定的狀態或指定的逾時到期。

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)

參數

desiredStatus
ServiceControllerStatus

要等候的狀態。

timeout
TimeSpan

TimeSpan 物件,指定等候服務到達指定狀態的時間長度。

例外狀況

desiredStatus 參數不是 ServiceControllerStatus 列舉中定義的任何一個值。

針對 timeout 參數指定的值已到期。

備註

使用 WaitForStatus 暫停應用程式的處理,直到服務達到必要的狀態為止。

注意

方法 WaitForStatus 會在每個狀態檢查之間等候大約 250 毫秒。 WaitForStatus 無法偵測觀察到的服務變更為 desiredStatus 的情況,然後立即在該間隔中另一個狀態。

另請參閱

適用於