Some services don't need controlling; they start when the system boots and run until the system is shut down. You can set a service to start automatically either by setting the ProjectInstaller object's StartType
property or through the Services window of the Computer Management applet.
You can control a service using the ServiceController
class. By including an instance of this class in your application (this must be a separate application from the Windows service application itself), you can:
- Start or stop the service
- Pause and continue the service (only if the service's CanPauseAndContinue property is True)
- Send custom commands to the service
- Retrieve information from the service
There are three ways to add an instance of the ServiceController
class to your project:
- In the VS.NET Server Explorer, open the Servers node, open the desired server, then open the Services list. Drag the desired service onto your form.
- Open the Components tab of the Toolbox and drag the ServiceController component onto your form. Set the component's MachineName property to the name of the server where the service resides (use "." for the local machine), and set the ServiceName property to the name of the service.
- Create an instance of the ServiceController class in code, then set its MachineName and ServiceName properties as described above.
Because the Servers node of the Server Explorer is not available in Visual Basic Standard Edition or Visual C# Standard Edition, you must use the second or third technique to add a ServiceController
component to your project.
After connecting your ServiceController component to the service, you can:
- Use the ExecuteCommand() method to send a custom command to the service. The service responds to these commands in the OnCustomCommand() method.
- Set the service's CanPauseAndContinue, CanStop, and CanShutDown properties.
- Use the Start(), Pause(), Continue(), and Stop(), methods to control the service.
- Create a proxy through which you can communicate with and retrieve information from the service.
The controlling application can be a standard Windows application, or can display itself as an icon in the system tray from which the user can select commands and options. It could also be an ASP.NET application that permits a remote user to monitor and administer a service running on a remote system.