Monitoring and Controlling the Service C# Help

To monitor and control services, you can use the Services MMC snap-in that is part of the Computer Management administration tool. Every Windows system also has a command-line utility, net. exe, which allows you to control services. Another command-line utility is sc . exe. This utility has much

When you double-click QuoteService, you will get the Properties dialog box shown in Figure 23-16. This dialog box enables you to view the service name, the description, the path to the executable, the startup type, and the status. The service is currently started. The account for the service process can be changed with the Log On tab in this dialog.

MMC Computer Management

Using the Services snap-in to the Microsoft Management Console (MMC), you can view the status of all services. It is also possible to send control requests to services to stop, enable, or disable them, as well as to change their configuration. The Services snap-in is a service control program as well as a service configuration program.

When you double-click QuoteService, you will get the Properties dialog box shown in Figure 23-16. This dialog box enables you to view the service name, the description, the path to the executable, the startup type, and the status. The service is currently started. The account for the service process can be changed with the Log On tab in this dialog.


The Services snap-in is easy to use, but the system administrator cannot automate it because it is not usable within an administrative script. To control services, you can use the command-line utility net. exe: net star.t shows all running services, net start service name starts a service, and net 5 top service name sends a stop request to the service. It is also possible to pause and to continue a service with net pause and net continue (only if the service allows it, of course).

Figure 23-17 shows the result of net start in the console window.


There is a little-known utility delivered as part of the operating system: sc . exe.

sc . exe is a great tool to play with services. Much more can be done with  exe than with the net. exe utility. With sc . exe, it is possible to check the actual status of a service, or configure, remove, and add services, as Figure 23-18shows. This tool also facilitates the installation of the service, if it fails to function correctly

Visual Studio Server Explorer

It is also possible to control services using the Server Explorer within Visual Studio; Services is below Servers and the name of your computer. By selecting a service and opening the context menu, a service can be started and stopped. This context menu can also be used to add a ServiceController class to the project. To control a specific service in your application, drag and drop a service from the Server Explorer to the Designer: a ServiceController instance is added to the application. The properties of this object are automatically set to access the selected service, and the assembly System. Service Process is referenced. You can use this instance to control a service in the same way you can with the application that you develop in the next section

ServlceController Class

In this section, you create a small Windows application that uses the ServiceController class to monitor and control Windows Services

Create a WPF application with a user interface as shown in Figure 23-19. The main window of this application has a list box to show all services, four text boxes to display the display name, status, type, and name of the service, and six buttons. Four buttons are used to send control events, one button for a refresh of the list, and one button to exit the application.

You can read more about WPF “Windows Presentation Foundation.”

Monltorln~ the Service

With the ServiceController class, you can get the information about each service. The following table shows the properties of the ServiceController class.

the sample application, the properties DisplayName, ServiceName, ServiceType, and Status are usea-to.display the service information. Also, CanPauseAndContinue and CanStop are used to enable or disable the Pause, Continue, and Stop buttons.

To get all the needed information for the user interface, the class ServiceControllerlnfo is created. This class can be used for data binding and offers status information, the name of the service, the service type, and the information about which buttons to c~ntrol the service should be enabled or disabled.

Because the class System. ServiceProcess: servi6eController is used, you must reference the assembly System. ServiceProcess.

ServiceControllerlnfo contains an embedded ServiceController that is set with the constructor of the ServiceControllerlnfo class. There is also a read-only property Controller to access the embedded ServiceController.

To display current information about the service, the ServiceControllerInfo class has the read-only properties DisplayName, ServiceName, ServiceTypeName, and ServiceStatusName. The implementation of the properties DisplayName and ServiceName just accesses the properties DisplayName and ServiceName of the underlying ServiceController class. With the implementation of the properties ServiceTypeName and ServiceStatusName, more work is done: The status and type of the service cannot be returned at easily because a string should be displayed instead of a number, which is what the ServiceController class returns. The property ServiceTypeName returns a string that represents the type of the service. The ServiceType you get from the property ServiceCntroller. ServiceType represents a set of flags that can be combined by using the bitwise ORoperator. The InteractiveProcess bit can be set together with Win320WnProcess and Win32Share Process. So, first it is checked if the Interactive Process bit is set before continuing to check for the other values. With services, the string returned will be “Wm32Service Process” or “Win32 Shared Process”:

The ServiceControllerlnfo class has some more properties to enable the Start, Stop, Pause, and Continue buttons: EnableStart, EnableStop, EnablePause, and EnableContinue. These properties return a Boolean value according to the current status of the service:

In the ServieeControlWindow class, the method RefrelhServiceList () gets all the services using ServieeController. GetServiees () for display in the list box.TheGetServices () method returns an array of ServieeController instances representing all WmdowsServicesinstalled on the operating system. The ServieeController class also has the static method GetDeviees () that returns a ServieeController array representing all device drivers. The returned array is sorted with the help of the genericArray. Sort () method.The sort is done by the DisplayName as is defined with the anonymous method that is passed to the Sort () method. Using Array. ConvertAll (), the ServieeController instances are converted to the type ServieeControllerlnfo. Here, an anonymous method is passed that

invokes the serviceContz:oller lnfo constructor for every ServiceController object. Last, the Service assigned to the DataContext property of the window for data binding.

The method RefreshServiceList () to get all the services in the list box is called within the constructor of the class ServiceControlWindow. The constructor also defines the event handler for the Click event of the buttons:

Now, you can define the XAMLcode to bind the information to the controls. First, a DataTemplate is defined for the information that is shown inside the ListBox. The ListBox will contain a Label where the Content is bound to the DisplayName property of the data source. Af> you bind an array of ServiceControllerlnfo objects, the property DisplayName is defined with the ServiceControllerlnfo class:

<oataTemplate x:Key=’listTemplate’>
<Label Content='{Binding Path=DisplayName}’/>

The ListBox that is placed in the left side of the Window sets the ItemsSource property to (Binding). This way, the data that is shown in the list is received from the DataContext property that was set in the RefreshServiceList ()method. The ItemTemplate property references the resource list Template

that is defined with the DataTemplate shown earlier. The property Is Synchronized With Current item is set to True so that the TextBox and Button controls that are inside the same Window are bound to the current item that is selected with the ListBox.


With the TextBox controls, the Text property is bound to the corresponding property of the ServiceControllerlnfo  instance. Whether the Button controls are enabled or disabled is also defined from the data binding by binding the ISEnabled property to the corresponding properties of the ServiceControllerInfo instance that return a  boolean value:


Controlling the ServIce

With the ServiceController class, you can also send control requests to the service. The following table explains the methods that can be applied.


The following code controls the services.Because the code for starting,stopping, suspending, and pausing  similar, only one handler is used for the four buttons:


Because the action of controlling the services( take some time, the cursor is switched to the wait cursor in the first statement. Then a Service (controller method is called depending on the prHsed button. With the Wait For Status () method, you are waiting to check that the service changes the status to the requested value, but you only wait 10 seconds maximum. After this time, the information in the ListBox is refreshed, and the same service as before  is selected, and the new status of this service is displayed.Figure 23-20 shows the completed, running application.


Figure 23-2


Posted on October 31, 2015 in Windows Services

Share the Story

Back to Top
Share This