How to: Perform Administrative Tasks on Services
You can perform a variety of standard administrative tasks on a service, including starting and stopping it, pausing it, and restarting it. You can perform these tasks in two ways: by using the Launch Services Manager command in Server Explorer/Database Explorer, or by calling methods on a ServiceController component instance that has been connected to an existing service.
Generally, the service author writes code that customizes the action associated with a specific task. For example, a service can contain code to respond to an OnPause command. In that case, the custom processing for the Pause task is carried out before the service is paused. For information on defining functioning for the service, see How to: Create Windows Services.
The particular tasks that are available for a service depend on its properties; for example, you can set the CanStop property for a service to false. This causes the Stop command to become unavailable on that particular service. If you try to stop the service from code, the system raises an error: "Failed to stop ServiceName."
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
To perform an administrative task from Server Explorer/Database Explorer
In Server Explorer/Database Explorer, if the server on which you want to perform the task is not already listed, add it. For more information, see How to: Access and Initialize Server Explorer/Database Explorer.
To open the Services console and access individual service options, right-click the Services node and click Launch Services Manager.
To perform an administrative task programmatically
Create an instance of the ServiceController class and configure it to interact with the service you want to administer. For more information, see How to: Create ServiceController Component Instances.
Call the appropriate method to start, stop, pause, or continue the service.
Tip
You might want to query the corresponding property for any of these commands to determine if the service will respond to your request; that is, you might want to create a conditional statement that queries the CanStop property on the service and determine whether it is set to true before you call the Stop method.
The following example shows how to evaluate whether a service can accept a given command before issuing the command.
If myController.CanStop Then myController.Stop() End If
if (myController.CanStop) { myController.Stop(); }
See Also
Tasks
How to: Create ServiceController Component Instances
How to: Retrieve Lists of Services
How to: Create Windows Services
Concepts
Introduction to Communicating with Existing Services
Reference
How to: Access and Initialize Server Explorer/Database Explorer