首页 > 开发 > 综合 > 正文

create and Control Windows Services--Control Your

2024-07-21 02:21:29
字体:
来源:转载
供稿:网友

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 )
{
compmon.pause();
}
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 );
    i++;
}
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 [email protected]

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表