services|window|進階 You've created your service—now it's time to create the service controller. Use the System.ServiceProcess namespace's ServiceController class to interface programmatically with the Service Control Manager to start, stop, pause, resume, and send custom commands to any installed service.
Figure 2 | Keep Your Service Under Control. Click here.
Connect to a service by supplying its name to the ServiceController constructor and, optionally, specifying the machine name where the service is installed. Once the ServiceController object has been created, controlling the service is as simple as calling the Start(), Stop(), Pause(), and Continue() functions. For instance, start the service you created earlier by passing the name of the service, "File Monitor Service," to the ServiceController constructor and call the Start() function. If you need to retrieve the service's current status, you can compare its Status property with the ServiceControllerStatus enumerated type:
ServiceController compMon = new ServiceController("File Monitor Service");
if( compMon.Status == ServiceControllerStatus.Running )
The service controller provided with this article might prove to be a useful utility (see the sidebar, "Build the Code"). Once run, the service controller places an icon in the system tray. Clicking on the icon brings up a popup menu that allows you to do two tasks: control a service and change the services listed (see Figure 2). For the file-monitor service, you can start, stop, pause, and resume it as well as reset the performance counter data you implemented as custom service commands. You change dynamically which services are listed on the popup menu by selecting Options, whose dialog lists currently installed services on the local system (see Figure 3). Specify the ones you want listed on the service controller menu by placing check marks next to them.
Figure 3 | Add Services to the Menu Dynamically. Click here.
Use the ServiceController class once again to get a list of services installed on the local system. The call to ServiceController.GetServices() returns an array of the service names. You can then enumerate and extract the names individually using an IEnumerator interface on the array. To fill the listbox within the Options dialog, you call the InsertItem() method provided by the listbox control:
IEnumerator ienum = ServiceController.GetServices().GetEnumerator();
int i = 0;
while( ienum.MoveNext() != false )
serviceList.InsertItem(i,((ServiceController) ienum.Current).DisplayName );
When the service controller is running, you can see the file-monitoring service in action (see Figure 4). Open the Administrative tools in the Control Panel and double-click on Performance to view the performance counter data. You must add the file-monitor performance counter to the graph manually by clicking on the toolbar's "+" button, selecting the "File Monitor Service" category, and adding the four file-monitor counters. Notice the graphs change as a result of system use.
Figure 4 | Give a Command Performance. Click here.
In this article, I showed you how to create Windows Services using .NET and VC#.NET. Although I used a simplified example, I presented key concepts to help you get started. You should also find the service controller utility invaluable as you execute and debug your service. I think you'll find out that services aren't that difficult after all.
Mark Schmidt is a software engineer at Hewlett-Packard. When he's not busy working or writing, he enjoys trying to keep up with his three active sons. He's recently found a new love for technical writing, so you can expect even more articles in the future. E-mail Mark at firstname.lastname@example.org.