The place where stuff Happens

Creating a simple windows service in C#. Part 1

SimpleService.rar (12.12 kb)

Hey, I was researching on services and on how to create them in C# . and this is what I learnt...

First we start by create an empty project in visual studio..

To work with services we need to add to references to the project, the System.ServiceProcess and System.Configuration.Install that are found under .NET tab.

System.ServiceProcess provides all classes that allow to implement,install and control windows services.

System.Configuration.Install provides classes that allow the developer to write custom installers for components.


To add a reference to a project , right click on the project and click on add reference. And windows will pop up. Go the .NET tab and find the reference and double click on it to added to the project.

Now create a class and I named mine MyService.cs and start by adding the required using.

  • Using System;
  • Using System.Diagnostics;
  • Using System.ServiceProcess; 
and added the base class reference for the ServiceBase. Like so..

In the constructor we set the name for us to identify in the system, we also use the constructor for Initialization code and we need the main method to tell the system to enter in the constructor.


And then we just add two methods onStart and onStop.

onStart this is where the service will enter so all methods and code need to be accessed from here.

onStop this is where the service will go when the service will stop, so this is the best place where to stop any thread or variables.


blog comments powered by Disqus