サービスが指定したステータスになるまで、無期限に待機します。
Overloads Public Sub WaitForStatus( _
ByVal desiredStatus As ServiceControllerStatus _)
[C#]
public void WaitForStatus(ServiceControllerStatusdesiredStatus);
[C++]
public: void WaitForStatus(ServiceControllerStatusdesiredStatus);
[JScript]
public function WaitForStatus(
desiredStatus : ServiceControllerStatus);
パラメータ
- desiredStatus
待機に対するステータス。
例外
例外の種類 | 条件 |
---|---|
InvalidEnumArgumentException | desiredStatus パラメータが、 ServiceControllerStatus 列挙体で定義されている値ではありません。 |
解説
サービスが目的のステータスになるまで、アプリケーションの処理を中断するには、 WaitForStatus を使用します。
使用例
[Visual Basic, C#, C++] ServiceController クラスを使用して Alerter サービスが停止しているかどうかを確認する例を次に示します。サービスが停止している場合、このコードはサービスを開始し、サービス ステータスが Running に設定されるまで待機します。
' 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
[C#]
// 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.");
}
}
[C++]
// Check whether the Alerter service is started.
ServiceController *sc = new ServiceController();
if (sc)
{
sc->ServiceName = "Alerter";
Console::WriteLine("The Alerter service status is currently set to {0}",
__box(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}.",
__box(sc->Status));
}
catch (InvalidOperationException *e)
{
Console::WriteLine("Could not start the Alerter service.");
}
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- 直前の呼び出し元の完全信頼。このメンバは、部分的に信頼されているコードから使用することはできません。詳細の参照先 : 部分信頼コードからのライブラリの使用
参照
ServiceController クラス | ServiceController メンバ | System.ServiceProcess 名前空間 | ServiceController.WaitForStatus オーバーロードの一覧 | Status