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 类检查警报器服务是否已停止。 如果服务已停止,该示例将启动服务,并等待服务状态设置为 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 该间隔中的另一个状态的情况,然后立即变为另一个状态。

另请参阅

适用于