Click here to Skip to main content
Licence 
First Posted 9 Aug 2005
Views 24,762
Bookmarked 14 times

Using Windows Service

By | 9 Aug 2005 | Article
Using Windows Services to get a list of all Services running on the Localmachine and to Start or Stop the Respective Services

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.

 

 

 

 

 

 

 

 

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tanmoy Moitra



India India

Member

None

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA little mistake Pinmemberalf97414:13 15 Dec '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Aug 2005
Article Copyright 2005 by Tanmoy Moitra
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid