15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

684 ❘ ChaPTer 25 windOws services<br />

When you double-click QuoteService, you get the<br />

Properties dialog box shown in Figure 25-12. This dialog<br />

box enables you to view the service name, the description,<br />

the path to the executable, the startup type, <strong>and</strong> the<br />

status. The service is currently started. The account for<br />

the service process can be changed with the Log On tab in<br />

this dialog box.<br />

net.exe utility<br />

The Services snap-in is easy to use, but the system<br />

administrator cannot automate it because it is not usable<br />

within an administrative script. Controlling services with<br />

a tool that can be automated with a script, you can use the<br />

comm<strong>and</strong>-line utility net.exe. The net start comm<strong>and</strong><br />

shows all running services, net start servicename<br />

starts a service, <strong>and</strong> net stop servicename sends a stop<br />

request to the service. It is also possible to pause <strong>and</strong> to<br />

continue a service with net pause <strong>and</strong> net continue<br />

(only if the service allows it, of course).<br />

sc.exe utility<br />

Another little-known utility delivered as part of the operating system is sc.exe. This is a great tool to play<br />

with services. Much more can be done with sc.exe than with the net.exe utility. With sc.exe, you can<br />

check the actual status of a service, or configure, remove, <strong>and</strong> add services. This tool also facilitates the<br />

deinstallation of the service, if it fails to function correctly.<br />

Visual studio server explorer<br />

You can also control services using the Server Explorer within Visual Studio by selecting Services. The<br />

Services item is listed in the tree view with the first element Servers; below that you can find the name of<br />

your computer, <strong>and</strong> below that there’s the Services item. By selecting a service <strong>and</strong> opening the context<br />

menu, you can start or stop a service. This context menu can also be used to add a ServiceController<br />

class to the project.<br />

To control a specific service in your application, drag <strong>and</strong> drop a service from the Server Explorer to the<br />

Designer: a ServiceController instance is added to the application. The properties of this object are<br />

automatically set to access the selected service, <strong>and</strong> the assembly<br />

System.ServiceProcess is referenced. You can use this instance to<br />

control a service in the same way you can with the application that you<br />

develop in the next section.<br />

Writing a Custom service Controller<br />

figure 25-12<br />

In this section, you create a small Windows application that uses the<br />

ServiceController class to monitor <strong>and</strong> control Windows Services.<br />

Create a WPF application with a user interface as shown in<br />

Figure 25-13. The main window of this application has a list box to<br />

show all services, four text boxes to display the display name, status,<br />

type, <strong>and</strong> name of the service, <strong>and</strong> six buttons. Four buttons are used<br />

to send control events, one button for a refresh of the list, <strong>and</strong> one<br />

button to exit the application. figure 25-13<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!