Create and Control Windows Services--Control Your Service[等級:進階]

來源:互聯網
上載者:User
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 )
{
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 mark_schmidt2@hp.com.



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.