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: through Server Explorer, or by calling methods on acomponent 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 ancommand. 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 .
The particular tasks that are available for a service depend on its properties; for example, you can set theproperty for a service to false. This renders the Stop command unavailable on that particular service; if you try to stop the service from Server Explorer, the necessary menu item appears dimmed. If you try to stop the service from code, the system raises an error: "Failed to stop <servicename.>"
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see.
To perform an administrative task from Server Explorer
In Server Explorer, add the server you want if it is not already listed. For more information, see.
Expand the Services node, and then locate the service on which you want to perform administrative tasks.
Right-click the name of the service, and click the task you want to perform.
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.
Call the appropriate method to start, stop, pause, or continue the service.
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 theproperty on the service and determine whether it is set to true before you call the method.
The following example shows how to evaluate whether a service can accept a given command before issuing the command.