ServiceController.WaitForStatus 方法

定义

等待服务达到指定状态。

重载

WaitForStatus(ServiceControllerStatus)

无休止的等待服务达到指定状态。

WaitForStatus(ServiceControllerStatus, TimeSpan)

等待服务达到指定状态或等待指定的超时过期。

WaitForStatus(ServiceControllerStatus)

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
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)

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
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 ,然后立即变为另一种状态的情况。

另请参阅

适用于