Export (0) Print
Expand All

ServiceController.Start Method ()

.NET Framework 1.1

Starts the service, passing no arguments.

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

Exceptions

Exception Type Condition
Win32Exception An error occurred when accessing a system API.
InvalidOperationException The service cannot be started.

Remarks

You cannot call Stop for the service until the service controller status is Running.

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] 

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

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

ServiceController Class | ServiceController Members | System.ServiceProcess Namespace | ServiceController.Start Overload List | Stop | Status

Show:
© 2015 Microsoft