Export (0) Print
Expand All

ServiceController.Start Method

Starts the service.

Overload List

Starts the service, passing no arguments.

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

Starts a service, passing the specified arguments.

[Visual Basic] Overloads Public Sub Start(String())
[C#] public void Start(string[]);
[C++] public: void Start(String*[]);
[JScript] public function Start(String[]);

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 Start. 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