Click here to Skip to main content
6,597,576 members and growing! (22,967 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Windows Service Management

By Agus Kurniawan

Simple application to manage and control Windows Service
C#, Windows, .NET 1.0, Dev
Posted:4 Apr 2002
Views:116,261
Bookmarked:65 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.31 Rating: 4.23 out of 5
1 vote, 8.3%
1
2 votes, 16.7%
2
1 vote, 8.3%
3
1 vote, 8.3%
4
7 votes, 58.3%
5

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 ServiceController[] services;

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


Member
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 consultant and architect for People Enterprise (www.PeopleEnterprise.com). He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus
Occupation: Web Developer
Location: Indonesia Indonesia

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralThank you PinmemberLakshmipathy20:50 1 Mar '09  
QuestionAbout Service controller PinmemberPradeep Sattikar20:17 25 Sep '07  
Questionto access window service of client from server computer Pinmemberkk_upadhyay3:23 31 Jul '07  
QuestionIssue! Pinmembermoin0210:57 7 Nov '06  
Generalwindows service with notifyicons and contextmenu. Help! Pinmemberchinimimita3:16 17 Dec '04  
GeneralRe: windows service with notifyicons and contextmenu. Help! PinmemberXiangyang Liu3:47 17 Dec '04  
GeneralRe: windows service with notifyicons and contextmenu. Help! Pinmemberchinimimita13:16 20 Dec '04  
GeneralHow to add a new service or delete a service? PinmemberJian12345676:03 9 Sep '04  
GeneralRe: How to add a new service or delete a service? PinmemberXiangyang Liu3:38 17 Dec '04  
GeneralWow. PinsussAnonymous21:14 22 Sep '02  
GeneralRe: Wow. PinmemberTim Hodgson4:19 12 Nov '02  
GeneralRe: Wow. PinsussSi Downes23:50 15 Jan '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Apr 2002
Editor: Chris Maunder
Copyright 2002 by Agus Kurniawan
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project