Schedule your tasks with a windows service

If you want to schedule something to run or your program takes long time to process, Sometimes we may have to create a seperate service application,
What i happened to write this post is, My client wanted me to write an application to send emails in specific time periods, So to achieve that I thought of writing a windows service application.
We can install this service in your server or local computer and configure it to run on intervals using a timer. Let’s start coding.

Create a Windows service

When we create a windows service from visual studio, It gives us a service class with constructor method, OnStart and OnStop methods as follows.

default methods

Scheduling the task with a timer

I need to periodically perform a task, for that i need a timer component, We can add a timer into our service design from toolbox

Adding a timer

Fill up OnStart method

we need to initialise timer component inside the OnStart method, then I set timer interval and added a elapsed event to track each interval.

onStart method

Actual scheduling done in here

inside timer elapsed event, actual task is going to run. elapsed event is called for each 2 minutes according to this code sample

timer Elapsed method

How to stop the execution

to stop the execution, OnStop event is used.

stop event

How to install the windows service

Before installing the service in your pc, we need to create a installer class,

Installer class

Add installer class as shown above

Installer Class

in Installer class, we need to give a service name, so we can identify the service by its name.

Installer class code

Let’s install the service

service is getting created after we build the application. we can find the .exe file in the bin folder. But we can’t install the service by double clicking on it
we have to install it to the local pc or server


InstallUtil.exe is used to install/uninstall services in local machine or server. We can run installUtil command from Developer command prompt,
type InstallUtil.exe with full path to the service exe file.


But this doesn’t going to install the service, It throws an error.

Installutil permission issue

It seems like we don’t have access to install the service, I had to spend few minutes to find the issue,
run Developer commad prompt as administrator, then this permission issue is getting resolved.

We can install a service using cmd as well,

installutil in cmd

run cmd as administrator, then we can install the service using cmd as well.

View services

We can check available services, by just typing services in your program list.

logic of the service

I gave service name as ‘test service’ in installer class.

Windows service on Local Computer started and then stopped

I tried to start the service from the panel, But it didnt work,
I came across with this issue as well, spent lot of hours to find the issue, this issue comes normally when code has some kind of a error. It’s somewhat difficult to debug a windows service application to find out errors,

Debug a windows service application easily

normally in a windows service, program.cs looks like this.

program.cs file

If we want to check whether our service implementation has any issue, we can use this trick

manually start the service

manually start a method that has exact same code as the service implementation.
inside of start method implementation, we can check whether service logic works properly.

logic of the service

using this trick, we can identify our service logic is working fine.

Another way of testing the program logic is, create another console application and call service logic method from console app and verify it works fine.

Debugger Launch

If we want to debug onStart method, we can launch the debugger in start method

launch the debugger

we can launch the debugger like this,

launch the debugger

we can debug the code like this.

launch the debugger

But in here specific file we attatched into debugger is getting loaded. So other files are not loaded. Only we can debug the service file as this code sample shows.

Windows service can’t connect to the database

In my service, i need to access to the database, But it gives me an error like this.

Login failed. Login failed for user ‘NT AUTHORITY\SYSTEM’

windows service is running on ‘NT AUTHORITY\SYSTEM’ login. this login doesn’t seem to have access to the database
To resolve this, add this in your connectionstring, Integrated Security=SSPI or else create a user account for ‘NT AUTHORITY\SYSTEM’ in your database server and give necessary permissions.

These are the guidelines to create a windows service, so it’s your time to try it.

You can find a sample windows service application from here,