services|window| advanced 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 M Anager 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 it name to the ServiceController constructor and, optionally, specifying the machine NA The me where the service is installed. Once the ServiceController object has been created, controlling the service is as simple as calling the Start (), Stop (), P Ause (), and Continue () functions. For instance, start the service your created earlier by passing the name of the service, "File Monitor service," to the Ser Vicecontroller constructor and call the Start () function. If you are need to retrieve the service's current status, your can compare its status and the ServiceControllerStatus Enumerated type:
ServiceController Compmon = new ServiceController ("File Monitor Service");
if (Compmon.status = = servicecontrollerstatus.running)
{
Compmon.pause ();
}
The service controller provided with this article might prove to is a useful utility (the "Build the Code"). Once Run, the service controller places a icon in the system tray. Clicking on the "icon brings up a" popup menu that allows the "do two Tasks:control a service and" change the services Lis Ted (Figure 2). For the File-monitor service, you can start, stop, pause, and resume it as as-reset the performance counter data you Implemented as custom service commands. Dynamically which services are listed on the popup menu by selecting Options, whose dialog lists currently alled services on the Local System (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 a IEnumerator 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);
i++;
}
When the service controller are running, you can be the File-monitoring service in action (the 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 from 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 is the system use.
Figure 4 | Give a Command performance. Click here.
In this article, I showed you and the 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 are should also find the Service Controller utility invaluable as you execute and debug your service. I would have to find out that services aren ' t this difficult after all.
Mark Schmidt is a software engineer at Hewlett-Packard. When he is not busy working or writing, the He enjoys trying to keep the up and 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 mark_schmidt2@hp.com.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.