Click here to Skip to main content
Click here to Skip to main content

Windows Service Management

, 4 Apr 2002
Rate this:
Please Sign up or sign in to vote.
Simple application to manage and control Windows Service

Introduction

When I was reading the MSDN.NET library, I found an interesting topic about windows services. So I tried to build an application by myself about windows service management. This idea comes from the service application that Microsoft has built in the control panel. I hope this application can be become useful.

Building the Application

To build this application I use ServiceController class. This class needs namespace System.ServiceProcess. You can check on your MSDN libary. Here's these steps:

1. New Application C#, specify WinServiceManage as the project name with Window Application (figure 1)

Figure 1

2. Add a Reference on your application, click menu Project -> Add Reference and choose System.ServiceProcess.dll

Figure 2

3. Build your GUI like figure 3

Windows forms:

  • Label

  • Button

  • Panel

  • ListView

 

Figure 3

Properties of ListView:

Properties Value
Name ServiceList
FullRowSelect True
GridLines True
Columns look figure 4
MultiSelect False
Sorting Ascending
View Detail

The following shows the properties of the ListView (click button ...). Add 4 column with property:

Column Propertie Propertie value
Name Name ServiceName
  Text Service Name
  Width 126
Description Name Description
  Text Description
  Width 214
Status Name Status
  Text Status
  Width 74

Figure 4

4. Add this line of code on the top of file form1.cs    

  using System.ServiceProcess;

and add a variable on Form1 class

  public <font >ServiceController[] services;</font>

5. This is the code when the "View All" button is clicked:

   try
   {
    ListViewItem datalist;    
    services = ServiceController.GetServices();   
    ServiceList.Items.Clear();  
    foreach(ServiceController service in services)
    {    
        datalist = new System.Windows.Forms.ListViewItem(service.ServiceName.ToString());
        datalist.SubItems.Add(service.DisplayName);    
        datalist.SubItems.Add(service.Status.ToString());
        ServiceList.Items.Add(datalist);    
    }                

   }
   catch(Exception er)
   {
    MessageBox.Show(er.Message,"Error Exception",MessageBoxButtons.OK, 
                          MessageBoxIcon.Error); 
   }

6. Add Method with right mouse clicking on class view. Method name is List

Figure 5

Figure 6

Here's the code:

  public void ListChange()
  {
    if(ServiceList.SelectedItems.Count!=0)
    {
        ListViewItem item = ServiceList.FocusedItem;
        if(item!=null)
        {
            if(item.SubItems[2].Text=="Running")
            {
                StartBtn.Enabled = false;
                StopBtn.Enabled = true;
            }
            else
            {
                StartBtn.Enabled = true;
                StopBtn.Enabled = false;
            }
        }    
    }
  }

7. Method on Event of ListView ie: Click, KeyDown, and KeyUp

 ListChange();

8. The code line when start button is clicked:

 private void StopBtn_Click(object sender, System.EventArgs e)
 {

    try
    {
        if(ServiceList.SelectedItems.Count!=0)
        {
            int n = ServiceList.FocusedItem.Index;  
            if(services[n].CanStop)
            {
                services[n].Stop();
                StartBtn.Enabled = true;
                StopBtn.Enabled = false;
                ServiceList.FocusedItem.SubItems[2].Text = "Stopped";  
            }
            else
            {
                MessageBox.Show("This service couldn't be stopped","Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }                 
        }
        else
        {
            MessageBox.Show("Please,choose a service that you want to stop",
                            "Information",MessageBoxButtons.OK, 
                            MessageBoxIcon.Information);
        }
    }
    catch(Exception er)
    {
        MessageBox.Show(er.Message,"Error Exception",MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
 }

9. The code line when stop button is clicked:

  private void StartBtn_Click(object sender, System.EventArgs e)
  {
    try
    {
        if(ServiceList.SelectedItems.Count!=0)
        {
            int n = ServiceList.FocusedItem.Index;      
            services[n].Start();
            StartBtn.Enabled = false;
            StopBtn.Enabled = true;
            ServiceList.FocusedItem.SubItems[2].Text = "Running"; 
        }
        else
        {
            MessageBox.Show("Please,choose a service that you want to stop",
                            "Information",MessageBoxButtons.OK, 
                            MessageBoxIcon.Information);
        }
    }
    catch(Exception er)
    {
        MessageBox.Show(er.Message,"Error Exception",MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
  }

10. Run this application

Reference

MSDN .NET library

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

Agus Kurniawan
Founder PE College
Indonesia Indonesia
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly,and ASP/ASP.NET. He's founder for PE College(www.pecollege.net), free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus and http://blog.aguskurniawan.net

Comments and Discussions

 
GeneralThank you PinmemberLakshmipathy1-Mar-09 19:50 
QuestionAbout Service controller PinmemberPradeep Sattikar25-Sep-07 19:17 
Questionto access window service of client from server computer Pinmemberkk_upadhyay31-Jul-07 2:23 
QuestionIssue! Pinmembermoin0216-Nov-06 23:57 
Generalwindows service with notifyicons and contextmenu. Help! Pinmemberchinimimita17-Dec-04 2:16 
GeneralRe: windows service with notifyicons and contextmenu. Help! PinmemberXiangyang Liu17-Dec-04 2:47 
GeneralRe: windows service with notifyicons and contextmenu. Help! Pinmemberchinimimita20-Dec-04 12:16 
QuestionHow to add a new service or delete a service? PinmemberJian12345679-Sep-04 5:03 
AnswerRe: How to add a new service or delete a service? PinmemberXiangyang Liu17-Dec-04 2:38 
GeneralWow. PinsussAnonymous22-Sep-02 20:14 
GeneralRe: Wow. PinmemberTim Hodgson12-Nov-02 3:19 
GeneralRe: Wow. PinsussSi Downes15-Jan-03 22:50 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 5 Apr 2002
Article Copyright 2002 by Agus Kurniawan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid