.NET Framework: Service Controller

This sample shows how to control Windows Services under Windows NT, Windows 2000, or Windows XP.

Featured Highlights

Windows Services are long running programs (analogous to UNIX daemons) that perform task without user interaction. Services are controlled by a system service know as the Service Control Manager (SCM). This sample uses the ServiceController class from the System.ServiceProcess namespace.

Using the UI presented you can Start, Stop, Pause, and Resume a service. The sample checks the current state of the service to see what buttons should be enabled. A timer control runs every 5 seconds by default refreshing the status of all running services. Note that it does not re-enumerate the list of possible services. If you install or uninstall a service while the program is running, you will need the application to re-enumerate the list of possible services. This is accomplished by selecting Relist All Services from the Tools menu. The timer can be toggled on & off by checking the Auto Refresh check box. You can manually refresh the status of all services by pressing F5 or selecting Refresh from the Tools menu.

Requirements

Requires the Trial or Release version of Visual Studio .NET Professional (or greater). Services only run on Windows operating systems based upon the Windows NT kernel.

Running the Sample

Start the application and it will enumerate all services and show thier status and type. By selecting a different service, the buttons at the bottom of the screen will change their status. Clicking an enabled button will cause it to perform the action requested.

Note that a service can take a few seconds to adjust its state depending upon what it is doing when you request a state change.

Last Update: 15 July 2002