ServiceController.WaitForStatus 方法

定義

等待服務達到指定狀態。

多載

名稱 Description
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 暫停申請處理,直到服務達到所需狀態。

Note

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 暫停申請處理,直到服務達到所需狀態。

Note

WaitForStatus該方法在每次狀態檢查之間大約等待 250 毫秒。 WaitForStatus 無法偵測到該服務在該區間內切換到該 desiredStatus 狀態後立即切換到其他狀態的情況。

另請參閱

適用於