Download Code


Some times we need that a task repeats after a specific duration automatically i.e. every after 1 hour. In this case we can not create a Window application or Web Application because both require some user interfarience. I case of Window application we need to LogOn to the machine on which the application is installed. But if the machine is server class machine then it is not possible to log on the whole day. And in case of Web Application user needs to open the web page to do that particular task.


In such cases, window service comes into the picture. Window service is the application which runs continously in the background and on the basis of timer event perform the needful tasks.

Here I am describing you the steps involved in How to create a simple Window Service and How to register it into Services.

Step 1

Create a New Project of Type Window Service as shown in Figure 1

Figure 1

Service1.cs file will be opened in Design View.

Step 2

Right click in the Design View of Service1.cs file and click on Add Installer as shown in Figure 2

Figure 2

It will add one more file in the project named ProjectInstaller.cs. Open ProjectInstaller.cs in Design View.

Step 3

Goto ProjectInstaller.cs file. There will be two controls. One is serviceInstaller1 and second is serviceProcessInstaller1.

Open the properties of serviceInstaller1. Change StartType from Manual to Automatic. You can also change the Service Name, Description and Display Name.

Open the properties of serviceProcessInstaller1. Change Account from User to Local System.

Step 4

On OnStart and OnStop event write the code for EventLog entry.

protected override void OnStart(string[] args)
   EventLog.WriteEntry("Simple Window Service has Started......");

protected override void OnStop()
   EventLog.WriteEntry("Simple Window Service has Stopped......");

Step 5

Again goto Service1.cs file and add a Timer control from the Toolbox.

Note: Time control should be of type System.Timer. If it is not present in the Toolbox then you can add it by right clicking in the Toolbox and click on Choose Items. Under .Net Components you will find it.

Open the Time control properties and set the Interval property in Miliseconds. Add Timer event Timer_Elapsed and put you code here which you want to execute after specific interval.

In this example I am showing the Message box after every 5 minutes.

Step 6

Compile and Build your Window Service. Now your Window Service is ready to install in the Services. 

Step 7

For installing this you need InstallUtil.exe.

Open the command prompt using Run As Administrator option.

Goto the location C:\Windows\Microsoft.NET\Framework\v2.0.50727.

Type the following command: InstallUtil.exe <FullPath of Service exe file> and press enter.

A dialog box will appear asking for User Name and Password. Specify Window Current User Name and password and click on OK.

Step 8

Open Services by using services.msc command. Locate your Window Service.

Right Click on the service and Click Start. It will Start the Service.

Step 9

Open the Event Viewer for checking the Event entry. There should be Two event log entries in the Application event log list. One is generated by the system and second is by Window Service itself because we have write the code on OnStart event of the service.

This is the simple Window Service. You can follow these steps to create you own Window Service. You can also download the code of the Simple Window Service.

About This Article

Copyrights 2020,