Export (0) Print
Expand All

Performing 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: via 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 Creating 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.>"

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 Accessing and Initializing Server Explorer.
    Note   The Servers node of Server Explorer is not available in the Standard Edition of Visual Basic and Visual C# .NET. For more information, see Visual Basic Standard Edition Features or Visual C# Standard Edition Features.
  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 Creating ServiceController Component Instances.
  2. 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 Start method.

    The following example shows how to evaluate whether a service can accept a given command before issuing the command.

    ' Visual Basic
    If myController.CanStop Then
       myController.Stop()
    End If
    
    // C#
    if (myController.CanStop)
    {
       myController.Stop();
    }
    

See Also

Introduction to Communicating with Existing Services | Creating ServiceController Component Instances | Retrieving Lists of Services | Accessing and Initializing Server Explorer | Creating Windows Services

Show:
© 2015 Microsoft