Introducing Windows Services
Published: 15 Jun 2007
This article briefly examines the concept of Windows Services.
by Uday Denduluri
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25853/ 59


A service is a long running executable program that does not support any user interface. A service program runs under the system account. The system account is used by the operating system and by services that run under Windows. The system account (Internal account) was designed since many services and processes within Windows need the capability to log-in internally. ServiceProcessInstaller class installs the windows service. While installing, it specifies the user account under which the service will run. The instance of the ServiceProcessInstaller takes a property of the type Account. Account is of the type "ServiceAccount" which is of the type enum. The values of ServiceAccount enum are shown below.

Enum Type



Uses anonymous credentials


High privilege


Extensive local privileges 


Very specific to user

ServiceBase Class

The base class for all the windows services is ServiceBase class. ServiceBase class has methods like OnStart, OnStop, OnPause and OnContinue which can be overridden by the derived classes. Let us see the ServiceBase class in detail and understand it before we create a windows service. When the service starts the OnStart method of the service runs. The Service Control manager is responsible for running the Service. All the events, like OnStart, OnStop, etc. are accessed by the Service control Manager. It is to be understood that this is not similar to normal executable file execution. The executable initially calls the derived class constructor of ServiceBase. After this the OnStart method takes on. It is to be understood that all the resources or objects that are needed to be cleared off in the Onstop event should be created in Onstart event and not the constructor.

The following are the methods or events of ServiceBase class. All these events can be overridden.

1.    OnStart – This method is fired when the service is started by the Service Control Manager. This method is used to specify the processing that occurs when a Start command is received.

2.    OnStop – This method is called when the service is stopped by the Service Control Manager. This is typically used to specify the processing that occurs when a Stop command is received by the Service Control Manager.

3.    OnPause – This method is called when the service is paused and typically contains the processing to pause the service.

4.    OnContinue – This method is called when the service is resumed after being paused. This method typically contains the necessary processing so as to enable a service to return to normal functioning after the same was paused earlier.

5.    OnShutdown – This method is called when the system is being shutdown and contains the necessary processing indicative of what should happen prior to the system shutting down.

6.    OnPowerEvent – This method is used to specify the necessary processing that should take place when the power status changes.

7.    OnCustomCommand – This method is used to specify a custom command, i.e., any command other than those discussed above.

Creating and Installing a Windows Service with Visual Studio 2005

Creating and installing a windows service is explained step by step.

·         Open Visual Studio 2005

·         Select new project Visual C# - Windows in Project Types and Windows service in the installed templates and click Ok.

·         Write the code for OnStart and OnStop. Build and see that the windows service does not have any errors.

·         Run InstallUtil /i WindowsService.exe in the Visual Studio command prompt. To uninstall we can use InstallUtil /u WindowsService.exe.

·         Open Services.msc and locate the service in the list of the services. Start the service.



This article briefly examines the installation and execution of Windows Services.

User Comments

Title: good   
Name: junaid.iqbal
Date: 2011-07-02 4:57:04 AM
very good start
Title: Good   
Name: Ashok
Date: 2008-09-30 2:10:22 PM
very good to start with.
Title: Suggestion   
Name: Everson
Date: 2008-06-13 8:49:11 AM
Post here a draw of tipical windows service and then explain the theory about it.
Title: Well   
Name: Nitin Tiwari
Date: 2008-01-23 8:26:18 AM
this is good for starting..
Title: good article   
Name: good article
Date: 2007-10-04 2:56:56 AM
Nice aricle
Title: depsre articol   
Name: OMU
Date: 2007-07-02 6:58:51 AM
a good quick introduction to Widows Services

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-11-29 9:35:32 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search