Online courses in ASP.NET MVC / Core, jQuery, Angular, and Design Patterns conducted by Bipin Joshi. Read more...
Learn ASP.NET MVC / Core, jQuery, Angular, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

<%@ Page %>

Controlling Windows Services

Introduction

In my last article I showed you how to create a Windows Services. This article is going to explain about monitoring the Windows Services in local and remote machines.

How to communicate with existing service?

You can communicate with existing service using ServiceController. Using ServiceController you can interact with a particular service, display list of services in remote or local machine and pass custom commands to the service. You can create ServiceController instance in several ways. In this article, I've created an instance using ServiceController class. You need to set two properties (MachineName and Name of the Service) while creating this instance through code. The default value for MachineName property is "." (local computer).

Creating the application

Open Visual Studio .NET and select Windows Application project and set the name as "WindowsServiceMonitor" and click Ok. Create the UI of this application as shown in the following screen.



Name the list box as lstServices and buttons as btnStart and btnStop. Add reference to System and System.ServiceProcess. Next, in the Main() function, Create an array of ServiceController class to store the list of services. ServiceController is having two methods to get the services - GetServices() and GetDevices(). Note that GetServices() method will return non device driver services whereas GetDevices() method will return device driver services. Following code shows how the method is used in our example.
arrServices = ServiceController.GetServices()
Note: The array arrServices will contain all the services of the local machine. To retrieve services from remote machine, pass machine name as parameter to the GetServices() method.
arrServices = 
ServiceController.GetServices("")]
To interact with a service, again you have to create a ServiceController instance by passing service name.
Dim srvController as new ServiceController(IISAdmin)
Once you get the instance of ServiceController you can now start, stop or pause the service. Following are the code for Start and Stop buttons. To check whether a service can stop or pause, Service Controller exposes properties such as CanStop and CanPauseAndContinue.
srvController.start()
If srvController.CanStop then
	srvController.Stop()
end if

Summary

I hope this article gives you an idea about monitoring services in local and remote machine. You can try other options like disabling the service, passing command line parameters to the service, displaying dependencies. To see the sample, download the source code and run it in your machine. In my next article, I'll explain you how to monitor process and service details (Simple task manager).



Chockalingam Tirupathi is working as Software engineer in bangalore. He is haivng 6 years of experience on Microsoft technologies. He is also a Microsoft certified professional.

Posted On : 08 Mar 2003



Tags : .NET Framework Windows Forms Components Administration Windows