Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Monitoring Window services in local and remote machine

Author: DevASP
Download Source Code : 653_WindowsServiceMonitor.zip

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 d...

This article is going to explain about monitoring the Windows Services in local and remote machines. 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).

 

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.

 

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 Can Stop, Can Pause and Continue.

 

Steps you will do.  

Start visual studio and create a new window application.

Set the following properties of the form object.

 

Name = “frmMonitor”

Text = “Window Service Moniter”

  

Drop a list box control on the form and set the following properties.

 

Name = “lstServices”

  

Drop four label controls on the form and set their following properties.

First label control.

 

Name = “Label1”

Text = “Service Name”

Font = “Verdana, 9pt, style=Bold’

  

Second label control. 

 

Name = “Label2”

Text = “Service Status”

Font = “Verdana, 9pt, style=Bold’

 

Third label control.

 

Name = “lblServiceValue”

Text = “”

Font = “Verdana, 9pt, style=Bold’

 

Fourth label control.

 

Name = “lblStatusValue”

Text = “”

Font = “Verdana, 9pt, style=Bold’

 

Drop two button controls on the form and set their following properties.

First button control

 

Name = “btnStart”

Text = “Start”

Second button control

 

Name = “btnStop”

Text = “Stop”

  

Open the code window of form and imports the following namespace.

 

Imports System.ServiceProcess

 

Declare the following two global array of type string and ServiceController as below.

  

Public arrServices() As ServiceController

 

    'An array to store service status

Public arrServiceStatus() As String = {"Continue Pending", "Paused", "Pause Pending", "Running", "Start Pending", "Stopped", "Stop Pending"}

  

In the load event of the form write the following code.

 

Dim iCounter As Integer

arrServices = ServiceController.GetServices()

For iCounter = 0 To arrServices.Length - 1

   lstServices.Items.Add(arrServices(iCounter).DisplayName)

Next

  

In the click event of the start button write down the following code to stat the service which is currently stopped or paused.

 

Try

arrServices(lstServices.SelectedIndex).Start()

arrServices(lstServices.SelectedIndex).Refresh()

btnStart.Enabled = False

btnStop.Enabled = True

lblStatusValue.Text = arrServiceStatus(arrServices(lstServices.SelectedIndex).Status)

Catch ex As Exception

     MsgBox("Error while starting service" + ex.ToString())

End Try

 

In the click event of the button write down the following code to stop the running service.

 

If arrServices(lstServices.SelectedIndex).CanStop Then

 

    arrServices(lstServices.SelectedIndex).Stop()

    btnStart.Enabled = True

    btnStop.Enabled = False

    lblStatusValue.Text = arrServiceStatus(arrServices(lstServices.SelectedIndex).Status)

 

End If

 

Run your application. The list box will be loaded with the currently running services of windows. Click any of the service in the list box to see its status either it is running, paused or stop and use the button to start or stop the services. 

Article Comments
i like this nice example and that is very help full me. thanks

1 thing is that i am a basically vb developer and i use Oracle as Backend. now i have 1 problem that when i start my exe that time it give connection error if oracle service is not started.
so i want to do that if oracle service is not started then i want to start it by this application and if thar is allready start then ignore this process.
so if u have any idea to find out this soluation in VB then pl. guide me

again i am vary thankfull to you

Posted on 1/10/2007 2:11:53 AM by Hasmukh Prajapati

This article help me to find the status of SQL Server on my pc.

Thanks for such a nice article.

Posted on 2/20/2007 1:20:59 AM by krishan

This is a very good example it helped me a lot to detect status of local services. Please help me to detect remote services from local system

Posted on 9/16/2007 4:14:56 AM by krishnapriya

Excellent illustration of the use of ServiceController.
Thanks it helped me.

Posted on 12/8/2007 10:40:28 AM by Sudhir Brahma

It is a good example.But if someone can help and tell me how to do this in java.

Posted on 2/27/2008 1:10:53 AM by divya

This is a good example but if someone can suggest to do this in java.

Posted on 2/27/2008 1:11:57 AM by Divya

this article is very good.but i try to modified the example code to retrieve window service in remote host, it having a problem. what the solution??

Posted on 3/22/2008 5:11:59 AM by mr. faizal

Net is very important. Many people know a lot about using vb.net but those who accept to do so are not numerous. You are among them. Thank you my Guy for your valuable help.

God bless you. Amen

Posted on 4/14/2008 11:34:06 AM by Aliou Diallo

Import System.ServiceProcess is not supported in window application. Creating project as windows service application is for creating new service only. Please help me. I need to develop a tool for service monitor.

Posted on 2/12/2010 1:15:57 AM by kasi

Anyone know how to look up specific services derived from a dictionary or a list using ServiceController in C#. I don’t want a list of all sevices running just a small list of 10 or so that mean something to me.
Thanks So much in advance.

Posted on 6/8/2010 4:20:14 PM by E

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Traversing Directory Trees Using Win32 System Services

Disclaimer - Privacy
© 2002-2017 DevASP.net