Share via


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

NoteNote

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 Visual Studio Settings.

To perform an administrative task from Server Explorer

  1. In Server Explorer, add the server you want if it is not already listed. For more information, see How to: Access and Initialize Server Explorer/Database Explorer.

  2. Expand the Services node, and then locate the service on which you want to perform administrative tasks.

  3. Right-click the name of the service, and click the task you want to perform.

To perform an administrative task programmatically

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

  2. Call the appropriate method to start, stop, pause, or continue the service.

    NoteTip

    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();
    }
    
    if ( myController.get_CanStop()  ) {
       myController.Stop();
    }
    

See Also

Tasks

How to: Create ServiceController Component Instances
How to: Retrieve Lists of Services
How to: Create Windows Services

Reference

How to: Access and Initialize Server Explorer/Database Explorer

Concepts

Introduction to Communicating with Existing Services