Windows Services
.Net simplifies the handling of Windows Services with
the inclusion of System.ServiceProcess namespace. This namespace contains
all that is required to handle a Windows Services, namely:
| *Create |
| * Install |
| * Start |
| * Stop |
| * Pause an Windows Services |
I have prepared a small application which
simulates the functionality that the Windows Service Control Manager which you
can invoke by clicking on :
Control
Panel -------> Administrative
Tools ----------> Services
Below is a Screen Shot of the Application.

To retrieve the List of Services,
System.ServicesProcess.ProcessController class is used. This Class have a static
method "GetServices" and "GetDevices".
These functions are used to populate listview at
form_load event. Below is given the code snippet.
=======================================================================================================
private void
Form1_Load(object
sender, System.EventArgs e)
{
servctrl =
ServiceController.GetServices() ;
int
cnt;
for(cnt=0;cnt<=servctrl.GetUpperBound(0)-1;cnt++)
{ listView1.Items.Add(servctrl[cnt].DisplayName.Trim());}
servctrl=
ServiceController.GetDevices();
for(cnt=0;cnt<=servctrl.GetUpperBound(0)-1;cnt++)
{listView1.Items.Add(servctrl[cnt].DisplayName.Trim());}
}
=======================================================================================================
Add the following line as the declaration section
of the winform:
private
System.ServiceProcess.ServiceController[] servctrl;
In the code servctrl object is used to store the
details of the Windows Services that have been retrieved by the "GetServices"
and "GetDevices" functions.
When any of the Listed services are clicked the
following code is executed:
========================================================================================================
private
void listView1_SelectedIndexChanged(object
sender, System.EventArgs e)
{
if
(listView1.SelectedItems.Count>0)
{
bool abc=
timer1.Enabled==false ? timer1.Enabled=
true:timer1.Enabled=
true ;
txtservnm.Text= listView1.SelectedItems[0].Text.Trim();
selservctrl =
new
ServiceController(txtservnm.Text);
if (selservctrl.CanStop==true)
{btnStop.Enabled=true;}
else
{btnStop.Enabled=false;}
if (selservctrl.Status.ToString
()=="Running")
{btnStart.Enabled=false;}
else
{btnStart.Enabled=true;}
lblstatus.Text= "Status of " +
selservctrl.DisplayName + ": " + selservctrl.Status.ToString();
}
}
========================================================================================================
The above code is used to get the Service on
which the operations are to be performed.
selservctrl = new
ServiceController(txtservnm.Text);
This line gets the Service which is to be handled
then on the Click of START button
=====================================================================================================================
private
void
btnStart_Click(object
sender, System.EventArgs e)
{
selservctrl.Start();
selservctrl.Refresh();
lblstatus.Text= "Status of " + selservctrl.DisplayName + ": " +
selservctrl.Status.ToString();
}
========================================================================================================
the Service is Started by using the "selservctrl"
object
Similarly when the Stop Button is clicked
====================================================================================================================
private
void
btnStop_Click(object
sender, System.EventArgs e)
{
selservctrl.Stop();
selservctrl.Refresh();
lblstatus.Text= "Status of " + selservctrl.DisplayName + ": " +
selservctrl.Status.ToString();
}
===================================================================================================================================
The Service is Stopped, again by using the same
object ["selservctrl"]
But as soon as the Service is Started or Stopped
the Service goes in "StartPending or StopPending" Status.
Hence to Update the Status of the Service a timer
Control has been taken which only updates the status of the Service.
====================================================================================================================================
private
void
timer1_Tick(object
sender, System.EventArgs e)
{
selservctrl.Refresh();
lblstatus.Text= "Status of " + selservctrl.DisplayName + ": " +
selservctrl.Status.ToString();
if (selservctrl.Status.ToString
()=="Running")
{btnStart.Enabled=
false;}
else
{btnStart.Enabled=
true;}
if (selservctrl.Status.ToString
()=="Stopped")
{btnStop.Enabled=
false;}
else
{btnStop.Enabled=
true;}
}
====================================================================================================================================
I hope this simple demonstration have given an Idea of the
capabilitities of System.ServiceProcess.ServiceController.
In recent future I would continue this series by
demonstrating how to create and Install Windows Services from the Scratch.