Export (0) Print
Expand All

ServiceController Class

Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.

For a list of all members of this type, see ServiceController Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.ServiceProcess.ServiceController

[Visual Basic]
Public Class ServiceController
   Inherits Component
[C#]
public class ServiceController : Component
[C++]
public __gc class ServiceController : public Component
[JScript]
public class ServiceController extends Component

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You can use the ServiceController class to connect to and control the behavior of existing services. When you create an instance of the ServiceController class, you set its properties so it interacts with a specific Windows service. You can then use the class to start, stop, and otherwise manipulate the service.

You will most likely use the ServiceController component in an administrative capacity. For example, you could create a Windows or Web application that sends custom commands to a service through the ServiceController instance. This would be useful, because the Service Control Manager (SCM) Microsoft Management Console snapin does not support custom commands.

After you create an instance of ServiceController, you must set two properties on it to identify the service with which it interacts: the computer name and the name of the service you want to control.

Note   By default, MachineName is set to the local computer, so you do not need to change it unless you want to set the instance to point to another computer.

Generally, the service author writes code that customizes the action associated with a specific command. For example, a service can contain code to respond to a ServiceBase.OnPause command. In that case, the custom processing for the Pause task runs before the system pauses the service.

The set of commands a service can process depends on its properties; for example, you can set the CanStop property for a service to false. This setting renders the Stop command unavailable on that particular service; it prevents you from stopping the service from the SCM by disabling the necessary button. If you try to stop the service from your code, the system raises an error and displays the error message "Failed to stop servicename."

Requirements

Namespace: System.ServiceProcess

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

Assembly: System.Serviceprocess (in System.Serviceprocess.dll)

See Also

ServiceController Members | System.ServiceProcess Namespace | ServiceBase | ServiceControllerStatus | ServiceType

Show:
© 2014 Microsoft