Export (0) Print
Expand All

Introduction to Communicating with Existing Services

You can use the ServiceController component to connect to and control the behavior of existing services. When you create an instance of the ServiceController component, you set it to interact with a specific Windows service. You can then use the component instance to start, stop, and otherwise manipulate the service. For example, you might create a ServiceController component and use it with a Timer component to start and stop a service based on a timed interval.

You can do several useful things with the ServiceController component. For example:

  • You can start and stop the service to which you are connected, pause it, or perform any of the other administrative actions to which the service is capable of responding. For more information, see Performing Administrative Tasks on Services.
  • You can query various properties of the service with which the component interacts and retrieve the property values. For example, you might determine whether the service can be stopped, paused, and continued by retrieving the value of the CanStop and CanPauseandContinue properties.
  • You can return a list of the services that exist on a particular computer.
  • You can specify a custom command to call on your service. For more information on custom commands, see MSDN Online.

The ServiceController component actually passes its requests for actions to the Services Control Manager, rather than to the service itself. The Services Control Manager then stops, starts, or pauses the service or performs other actions as specified. After the ServiceController has passed the request on to the Services Control Manager, it returns; it does not wait for the Services Control Manager to pass the request on to the service. Because of this, you should handle exceptions within the service class itself rather than in the ServiceController class.

Note   In general, you should not add a ServiceController component to a Windows Service application if your intent is for that component to manipulate the service contained in the same project. Code within the project that creates a service cannot be used to control the service; it must be controlled from another context.

See Also

Creating ServiceController Component Instances | Performing Administrative Tasks on Services | Retrieving Lists of Services

© 2015 Microsoft