Click here to Skip to main content
14,033,835 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 2 Apr 2006
Licenced CPOL

Mastering Windows Services

, 2 Apr 2006
Rate this:
Please Sign up or sign in to vote.
This is a series of articles in which we will explore and understand the architecture, working, installation, maintenance and issues of Windows services.


I know there are a number of articles on the Internet that talk about Windows Services, but unfortunately I didn't find any of the articles which we can call a complete Guide to Windows services. This made me come up with this series of articles.

In this series, we will explore and understand the architecture, working, installation, maintenance and issues of Windows services.

Before starting this article, let me present the whole outline which we are going to follow:

  • What is Windows service?
  • Windows service architecture
  • Namespace
  • Types of Windows service
  • States of Windows service
  • Programming model of Windows service
  • Creating Windows service
  • Installing, Uninstalling Windows service
  • Controlling Windows Service
  • Default properties of Windows service
  • Security Context of Windows Service

What is a Windows Service?

Windows services enable you to perform tasks that execute as different background processes. It executes in its own process space until a user stops it or computer shuts down. This kind of application does not have a user interface. It is installed in the registry as an executable object.

Windows Service Architecture

Now let’s have a look at the Windows service architecture. The Windows service architecture consists of three components:

Service Application

An application that consists of one or more services that provides the desired functionality.

Service Controller Application

An application that enables you to control the behavior of a service.

Service Control Manager

A utility that enables you to control the services that are installed on a computer.


The System.ServiceProcess namespace contains classes that enable you to create, install, implement and control Windows service. After you create Windows services, you use ServiceInstaller and ServiceProcessInstaller classes to install Windows service. You can view all the registered service applications in the Windows registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services.

Types of Windows Services

Windows services can be categorized on the basis of number of services running in process space. The service that uses a single process space is called Win32OwnProcess service, whereas the services that share a process with other services are called win32ShareProcess services.

States of Windows Services

The Windows services can be either in start, stop, paused and continue states. In addition, the services can be in pending state. A pending state indicates that a command was issued but not completed.

Programming Model of Windows Service Applications

You create a Windows service application by using the classes of the System.ServiceProcess namespace.


You override the methods in the ServiceBase class to create services. The methods of this class enable you to specify the behavior of your service application.

ServiceProcessInstaller and ServiceInstaller

These classes enable you to define the process of installing and uninstalling your service application.


The methods of this class enable you to manipulate the behavior of your service. You can use its methods to start, stop and control service.

Creating Windows Service

To create a Windows service, you create a class that extends the System.ServiceProcess.ServiceBase class. You override the functions of ServiceBase to customize the behavior of your service application.

public class DBWriter : System.ServiceProcess.ServiceBase 

I am summarizing these methods as follows:


You override this method to specify the tasks your service performs when it starts. You can set your service to start when the computer on which it is installed reboots. To do this, you set the StartType property of the service. You use the members of the servicestartmode enumeration to define whether the service automatically starts when the computer starts, starts when a user manually starts it or is disabled.

/// <span class="code-SummaryComment"><summary></span>
/// Set things in motion so your service can do its work.
/// <span class="code-SummaryComment"></summary></span>
protected override void OnStart(string[] args)
 //Create Transaction File and Close it
 FileStream fsTramsaction = File.Create ("c:\\Transaction.tmp");

//Create Customers DB File if it does not Exist
 if (! File.Exists("c:\\customers.db"))
  FileStream fsCustomers = File.Create ("c:\\customers.db");
 //Write to Transaction Log
 myLog.WriteEntry("DBWriter Service Started Successfully on " + 
        System.DateTime.Now.ToString() , EventLogEntryType.Information );


You override this method to specify the tasks your service performs when it pauses.


You override this method to specify the tasks your service performs when it stops. When the stop event occurs, the service control manager checks the value of the CanStop property and passes the Stop command to the onstop method on the basis of its value.

/// <span class="code-SummaryComment"><summary></span>
/// Stop this service.
/// <span class="code-SummaryComment"></summary></span>
protected override void OnStop()
 // Delete the Transaction.tmp File

 //Set the Performance Counter value to 0
 this.myPerformanceCounter.RawValue = 0;

 //Write Entry to Event Log
 this.myLog.WriteEntry("Service Stopped Successfully on " + DateTime.Now , 


You override this method to specify how your service behaves when it restarts after pausing.


You override this method to specify the tasks your service performs when computer is running on shuts down.


You override this method when you want a service to accept custom commands. The method takes an integer parameter on the basis of which a particular action is taken.

protected override void OnCustomCommand(int command)
 base.OnCustomCommand (command);
 switch (command)
  case(201) : Commit();break;
  case (200) : RollBack();break;


You override this method to specify how your service performs when it receives a power management event such as low battery.

In the next article, we will see how to Install and Uninstall Windows service, and Control Windows Service. We will also explore the security contexts of this type of application.


  • 2nd April, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Altaf Al-Amin
United Arab Emirates United Arab Emirates
Altaf Al-Amin Najvani
Project Manager
Commercial Bank Of Dubai

BS - Computer Science
Masters In Project Management
Masters In Business Administration

CISA - Certified Information Systems Auditor
ITIL (Foundation)
Microsoft Certified Technology Specialist (SQL Server 2005)
Microsoft Certified Technology Specialist (Web Applications)
Microsoft Certified Application Developer (.NET)
Microsoft Certified Solution Developer (.NET)

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Anton Lesnichenko15-Apr-10 20:55
memberAnton Lesnichenko15-Apr-10 20:55 
QuestionProblem in access (File.Exists("")) Pin
HrdkSanghavi13-Nov-08 22:17
memberHrdkSanghavi13-Nov-08 22:17 
QuestionProblem with Windows Service OnStop Pin
karthi845-Aug-08 0:41
memberkarthi845-Aug-08 0:41 
QuestionDo you know how to use form fields with services? Pin
Buddy Stein1-Jul-07 4:14
memberBuddy Stein1-Jul-07 4:14 
GeneralSome more useful information about Services Pin
Shindigo15-Jun-07 2:28
memberShindigo15-Jun-07 2:28 
QuestionPassing Data to a Service Pin
joycekc21-Dec-06 18:29
professionaljoycekc21-Dec-06 18:29 
AnswerRe: Passing Data to a Service Pin
Wolfram Steinke21-Jan-07 14:55
memberWolfram Steinke21-Jan-07 14:55 
AnswerRe: Passing Data to a Service Pin
motojojo28-Feb-07 23:30
membermotojojo28-Feb-07 23:30 
JokeOooh I was really Looking for this Pin
Agha.net13-Dec-06 19:22
memberAgha.net13-Dec-06 19:22 
Questionneed more basic info! (Installation) Pin
Princesse24-Nov-06 4:54
memberPrincesse24-Nov-06 4:54 

I made a basic windows service and when I try to install it with UtilInstall.exe, it gives me an exception telling the structure of the file AppService.exe is incorect. When I search more about the exception, it tells that the exe might have some unsafe code in it. But I didn't add anything to the basic windows services template made by VS2005!!

I need more informations about installation or stuff required to made a good installation.

The point of the article
- Installing, Uninstalling windows service

aren't explained in the article. Can you tell me more or refer me to a documentation about it please?

Thanks in advance!
Generalneed more Pin
MrsPuff20-Jul-06 5:32
memberMrsPuff20-Jul-06 5:32 
GeneralRe: need more Pin
AbbasHere17-May-07 12:43
memberAbbasHere17-May-07 12:43 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190423.1 | Last Updated 2 Apr 2006
Article Copyright 2006 by Altaf Al-Amin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid