Windows Services

Windows Service:

Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.

Create a Windows Service

Creating Windows Service is very easy with visual studio just follow the below steps to create windows service

Open visual studio –> Select File –> New –>Project–> select Windows Service

And give name as MyWindowsService after that rename Service1.cs to SchedulingService.cs


 After change name of service, open SchedulingService.cs in design view right click and select Properties now one window will open in that change Name value to SchedulingService and change ServiceName to SchedulingService. Check below properties window that should be like this


 also in design view of SchedulingService.cs right click and select Add Installer files to our application. Now you will see designer screen added to project with 2 controls: serviceProcessInstaller1 and ServiceInstaller1

 Main purpose of using Windows Installer is an installation and configuration service provided with Windows.

 Now right click on serviceProcessInstaller1 and select properties in that change Account to LocalSystem


After completion of setting all the properties now we need to write the code to run the windows services at scheduled intervals.

To add code to Windows Service

Right Click on SchedulingService.cs [Design] and select View Code, then you will see SchedulingService.cs

Now write below code,

publicpartialclassSchedulingService : ServiceBase


//Initialize the timer

Timer timer = newTimer();

public SchedulingService()




 protectedoverridevoid OnStart(string[] args)


//add this line to text file during start of service

TracingService(“Start Service”);

//handle Elapsed event

timer.Elapsed += newElapsedEventHandler(OnElapsedTime);

 //This statement is used to set interval to 1 minute (= 60,000 milliseconds)

 timer.Interval = 60000;

 //enabling the timer

timer.Enabled = true;


protectedoverridevoid OnStop()


timer.Enabled = false;

TracingService(“Stopping Service”);


 privatevoid OnElapsedTime(object source, ElapsedEventArgs e)


TracingService(“Entry at “ + DateTime.Now);


privatevoid TracingService(string content)


//set up a filestream

FileStream fs = newFileStream(@”d:\ScheduledService.txt”, FileMode.OpenOrCreate, FileAccess.Write);

 //set up a streamwriter for adding text

StreamWriter sw = newStreamWriter(fs);

 //find the end of the underlying filestream

sw.BaseStream.Seek(0, SeekOrigin.End);

 //adding the text



//close the writer




Now Build the application, which generates .EXE file.

How to Instal Windows Service ?

To install windows service in your system follow these steps

Start –>All Programs –>Microsoft Visual Studio 2008 –>Visual Studio Tools –>Open Visual Studio Command Prompt

Go to the path where .EXE generated.

Now type, installutil MyWindowsService.EXE

with this service was installed successfully.

Now Installed service will be started with below steps,

Start –>Control Panel –>Open Control Panel –>Select Administrative Tools–>Computer Management –>Services and Applications –>Services –>Open services

check for your service name and right click on that and select Start option.

Now You windows service is installed and started working.

How to stop or uninstall windows service ?

Installed service will be stopped with below steps,

Start –>Control Panel –>Open Control Panel –>Select Administrative Tools–>Computer Management –>Services and Applications –>Services –>Open services

check for your service name and right click on that and select Stop option.

If we want to uninstall the installed windows service you just point to your service same as what I explained previously and type statement installutil /u and your service name Ex : Installutil /u MyWindowsService.EXE