Export (0) Print
Expand All

ServiceController.WaitForStatus Method

Waits for the service to reach the specified status.

Overload List

Infinitely waits for the service to reach the specified status.

[Visual Basic] Overloads Public Sub WaitForStatus(ServiceControllerStatus)
[C#] public void WaitForStatus(ServiceControllerStatus);
[C++] public: void WaitForStatus(ServiceControllerStatus);
[JScript] public function WaitForStatus(ServiceControllerStatus);

Waits for the service to reach the specified status or for the specified time-out to expire.

[Visual Basic] Overloads Public Sub WaitForStatus(ServiceControllerStatus, TimeSpan)
[C#] public void WaitForStatus(ServiceControllerStatus, TimeSpan);
[C++] public: void WaitForStatus(ServiceControllerStatus, TimeSpan);
[JScript] public function WaitForStatus(ServiceControllerStatus, TimeSpan);

Example

[Visual Basic, C#, C++] The following example uses the ServiceController class to check whether the Alerter service is stopped. If the service is stopped, the example starts the service and waits until the service status is set to Running.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of WaitForStatus. For other examples that might be available, see the individual overload topics.
[Visual Basic] 

         ' 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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

ServiceController Class | ServiceController Members | System.ServiceProcess Namespace

Show:
© 2014 Microsoft