Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

Service Controller

Rate me:
Please Sign up or sign in to vote.
4.19/5 (15 votes)
29 Sep 20051 min read 111.3K   2K   33   14
How to programmatically control an NT Service.

User Interface

Introduction

Controlling services from code has never been so easy. The .NET framework has taken the cumbersome task of wrapping the Windows API calls that we traditionally used to control services.

Using the code

First, let's get a list of services running on the machine. To do this, we will call the static method GetServices() of the System.ServiceProcess.ServiceController class.

C#
//create an array to hold the list of services
System.ServiceProcess.ServiceController[] services;
//fill the array with the GetServices method
services = System.ServiceProcess.ServiceController.GetServices();

In my application, I load a ComboBox with the list of services running on the machine. To make life easy, I create a DataTable on the fly and add records to it to represent the individual services. Then I bind the DataTable that I created to the ComboBox. I also take some additional steps to sort the data in the DataTable and to add an initial ComboBox entry that prompts the user to "Select A Service".

C#
//Add columns to the DataTable
_tblServices.Columns.Add( new DataColumn( "ServiceName", typeof( System.String ) ));
_tblServices.Columns.Add( new DataColumn( "DisplayName", typeof( System.String ) ));
_tblServices.Columns.Add( new DataColumn( "Index", 
                              typeof( System.Int32 ) )); //used for sorting

System.ServiceProcess.ServiceController[] services;
services = System.ServiceProcess.ServiceController.GetServices();

foreach( System.ServiceProcess.ServiceController controller in services )
{
   DataRow row = _tblServices.NewRow();
   row["ServiceName"] = controller.ServiceName;
   row["DisplayName"] = controller.DisplayName;
   row["Index"] = 1;

   _tblServices.Rows.Add( row );
}

//add a dummy row
DataRow blankRow = _tblServices.NewRow();
blankRow["ServiceName"] = string.Empty;
blankRow["DisplayName"] = "<Select A Service>";
blankRow["Index"] = 0;
_tblServices.Rows.Add( blankRow );

//use a DataView to sort the entries. I use the "Index" 
//Column to force my blank record to the top
DataView view = new DataView( _tblServices, string.Empty, 
                    "Index, DisplayName", 
                    DataViewRowState.CurrentRows );
cbServices.DataSource = view;
cbServices.ValueMember = "ServiceName";
cbServices.DisplayMember = "DisplayName";

//Assign an event handler to the selectedValueChanged Event
cbServices.SelectedValueChanged += 
           new EventHandler(cbServices_SelectedValueChanged);

In the previous code, the ComboBox was assigned an event handler. The SelectedValueChanged event will allow me to update the GUI according to the status of the selected service. In order to get the status of the service I instantiate a new ServiceController object and pass in the Service Name. Then I enable/disable each button depending on the current service status.

C#
private void cbServices_SelectedValueChanged(object sender, System.EventArgs e)
{
    ServiceController controller = GetService();

    if( controller != null )
        UpdateControls( GetService() );
    else
    {
        statusBar1.Text = string.Empty;
        btnStart.Enabled = false;
        btnStop.Enabled = false;
        btnPause.Enabled = false;
        btnContinue.Enabled = false;
    }
}

//Updates the buttons according to the currently selected service
void UpdateControls( ServiceController controller )
{
    btnPause.Enabled = ( controller.Status == ServiceControllerStatus.Running 
                                 && controller.CanPauseAndContinue == true );
    btnStop.Enabled = ( controller.Status == ServiceControllerStatus.Running 
                                             && controller.CanStop == true );
    btnContinue.Enabled = ( controller.Status == ServiceControllerStatus.Paused 
                                 && controller.CanPauseAndContinue == true );
    btnStart.Enabled = controller.Status == ServiceControllerStatus.Stopped;
    statusBar1.Text = "Status: " + controller.Status.ToString();
}

//Gets the currently selected service
private ServiceController GetService()
{
    if( cbServices.SelectedValue.ToString().Trim() != string.Empty )
        return new ServiceController( (string)cbServices.SelectedValue );
    else
        return null;
}

If you are familiar with services you are probably aware that some services can not be stopped or paused. This can be checked by accessing the CanPauseAndContinue property and the CanStop property of the ServiceController class. Since my UpdateControls() method checks these properties I don't have to check these properties again during the button Click events.

C#
private void btnStop_Click(object sender, System.EventArgs e)
{
    System.ServiceProcess.ServiceController controller = GetService();
    controller.Stop();
    WaitForStatus( controller, ServiceControllerStatus.Stopped, 
                                        new TimeSpan( 0,1,0) );
    UpdateControls( controller );
}

private void btnStart_Click(object sender, System.EventArgs e)
{
    System.ServiceProcess.ServiceController controller = GetService();
    controller.Start();
    WaitForStatus( controller, ServiceControllerStatus.Running, 
                                        new TimeSpan( 0,1,0) );
    UpdateControls( controller );
}

private void btnPause_Click(object sender, System.EventArgs e)
{
    System.ServiceProcess.ServiceController controller = GetService();
    controller.Pause();
    WaitForStatus( controller, ServiceControllerStatus.Paused, 
                                       new TimeSpan( 0,1,0) );
    UpdateControls( controller );
}

private void btnContinue_Click(object sender, System.EventArgs e)
{
    System.ServiceProcess.ServiceController controller = GetService();
    controller.Continue();
    WaitForStatus( controller, ServiceControllerStatus.Running, 
                                        new TimeSpan( 0,1,0) );
    UpdateControls( controller );
}

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


Written By
Software Developer (Senior) Concepts2Code
United States United States
Michael is the co-founder and master consultant for Concepts2Code, a software consulting company based in Buffalo, New York. He's been programming since the early 1990's. His vast programming experience includes VB, Delphi, C#, ASP, ASP.NET, Ruby on Rails, Coldfusion and PHP. Michael also is a Microsoft Certified Application Developer and a Certified Technology Specialist for SQL Server.

Visit his blog.

Comments and Discussions

 
GeneralGetting error Pin
Arpit11720-Jul-09 2:47
Arpit11720-Jul-09 2:47 
GeneralRe: Getting error Pin
Michael Ceranski20-Jul-09 2:53
Michael Ceranski20-Jul-09 2:53 
GeneralRe: Getting error Pin
Arpit11720-Jul-09 3:17
Arpit11720-Jul-09 3:17 
GeneralGives Error in Windows Vista Pin
Tejal Bhavsar21-Jul-08 21:09
Tejal Bhavsar21-Jul-08 21:09 
QuestionRe: Gives Error in Windows Vista Pin
Michael Ceranski22-Jul-08 1:50
Michael Ceranski22-Jul-08 1:50 
AnswerRe: Gives Error in Windows Vista Pin
TheAsgard27-Jul-08 23:37
TheAsgard27-Jul-08 23:37 
Its a security issue. You need to have administrator rights or use Com Elevation. If you run the test program as administrator it works fine.

I am developing under Vista and I have had no end of issues like this. Works fine under XP but not under Vista and vice versa. Would be nice if this was all transparent.
GeneralRe: Gives Error in Windows Vista Pin
AndrewHeard1-Jul-12 15:35
AndrewHeard1-Jul-12 15:35 
QuestionCannot find the specific windows service Pin
bmwgamil25-Oct-07 10:35
bmwgamil25-Oct-07 10:35 
GeneralControlling Services on Remote Server Pin
Gideons3001-Jun-07 5:12
Gideons3001-Jun-07 5:12 
QuestionAdmission Control For WLAN (DHCP, Service Controller) ? Pin
jsyan8-Apr-07 11:30
jsyan8-Apr-07 11:30 
AnswerRe: Admission Control For WLAN (DHCP, Service Controller) ? Pin
kiquenet.com26-Apr-10 3:43
professionalkiquenet.com26-Apr-10 3:43 
AnswerRe: Admission Control For WLAN (DHCP, Service Controller) ? Pin
waqqas_jabbar3-Jan-11 22:50
waqqas_jabbar3-Jan-11 22:50 
QuestionService disabled Pin
vinay28may24-Jan-07 17:39
vinay28may24-Jan-07 17:39 
AnswerRe: Service disabled Pin
Chadwick Posey8-May-07 9:57
Chadwick Posey8-May-07 9:57 

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

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