Windows service with cSharp

Posted by

Windows service with Csharp

windows service C#


Before starting our topic on Windows service with Csharp , we have to understand what windows services are?
If we look in our task manager of windows, we will see number of services running. Some are essential for windows and some are running for other applications.Some are auto-start and some are automatic that start when the operating system is is loaded.
Generally, windows services have no user interfaces, they may be considered as services to operating system, applications or hardware etc. In a nutshell, a windows service is an invisible program that executes in the background of our operating system
Let us discuss something about operating system kernel

The kernel is a computer program that manages input/output of the software running in operating system
and send the instruction to the CPU so that it can realize what to do with this information

Here I have mentioned an excerpt from

When a computer program (in this case called a process) makes requests of the kernel, the request is called a system call. Various kernel designs differ in how they manage system calls (time-sharing) and resources. For example, a monolithic kernel executes all the operating system instructions in the same address space to improve the performance of the system. A microkernel runs most of the operating system’s background process in user space, to make the operating system more modular and, therefore, easier to maintain.

Now, lets talk about the basic attributes:

1. Service name
2. Service property such as onstart and onstop
3. Service log that contains the information regarding service and show any problems that have occured

We are now going to make a service

In your visual studio environment, open up c# empty project, then do the following

* In Solution Explorer, right-click the project and click add reference

* A .Net framework tab will appear and tick the System.dll file and System.ServiceProcess.dll
Finally click OK.

* Within the project, add a class named MyClass and Derive it with the System.ServiceProcess.ServiceBase


using System;
using System.ServiceProcess;

public class MyClass : ServiceBase

public MyClass()
this.ServiceName = “testService”;
this.CanStop = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;


protected override void OnStop()

protected override void OnPause()

protected override void OnContinue()

protected override void OnShutdown()

All the services are baseless if we do not derive from ServiceBase class, as lots of methods are present in it. The constructor of the class provides these attributes such as ServiceName, CanStop etc. in the current instance. Whenever the class constructor is invoked, we will get a new service.
then we have a Main() method.Since it executes first, so we are instantiating the call to thee constructor inside MyClass. We have overritten the defined methods such as OnStart() etc.

Hope all of you enjoyed. Our next post will be using threading model to tackle the services

Facebook Comments

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter