ServiceController.WaitForStatus メソッド

定義

サービスが指定された状態になるまで待機します。

オーバーロード

名前 説明
WaitForStatus(ServiceControllerStatus)

サービスが指定された状態になるまで無限に待機します。

WaitForStatus(ServiceControllerStatus, TimeSpan)

サービスが指定された状態に達するか、指定したタイムアウトが期限切れになるまで待機します。

WaitForStatus(ServiceControllerStatus)

サービスが指定された状態になるまで無限に待機します。

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)

サービスが指定された状態に達するか、指定したタイムアウトが期限切れになるまで待機します。

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 に変わり、その間隔ですぐに別の状態に変わるケースを検出できません。

こちらもご覧ください

適用対象