How to Create Windows Service using C#

In this document i want to explain how to create simple windows service using C#. I believe there are lots of document, help online that you can use, but here I will try to explain as simple as possible so every body can understand the basic concept.

Step 1: Create Windows Service Project

First Step is to create project, to do that go to visual C#, Windows and select Windows Service.

CreateWinServiceProject

This is how your project will look like after you create:

WinService

Step 2: Add Installer

In this stage we are going to add installer to the Service. To add installer in Service1.cs in design mode you have ro write click and Add installer.

addinstaller

After you successfully add the installer your project will look like this:
Installer

Step 3: Work In DEBUG mode:

If you run the project now, you will get windows service start failure.

Startfailure

To make your project work in debug mode without installing it, there are few lines of code that you have to add to your project.
Open Program.cs and modify the code:

Code Snippet
  1. namespace WindwosServiceTest
  2. {
  3.     static class Program
  4.     {
  5.         /// <summary>
  6.         /// The main entry point for the application.
  7.         /// </summary>
  8.         static void Main()
  9.         {
  10. #if DEBUG
  11.             Service1 myService = new Service1();
  12.             myService.OnDebug();
  13.             System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
  14.  
  15. #else
  16.             ServiceBase[] ServicesToRun;
  17.             ServicesToRun = new ServiceBase[]
  18.             {
  19.                 new Service1()
  20.             };
  21.             ServiceBase.Run(ServicesToRun);
  22. #endif
  23.         }
  24.     }
  25. }

Now open the Service1.cs and Add the following code:

Code Snippet
  1. public void OnDebug()
  2.       {
  3.           OnStart(null);
  4.       }

your Service1.cs should look like this now:

Code Snippet
  1. namespace WindwosServiceTest
  2. {
  3.     public partial class Service1 : ServiceBase
  4.     {
  5.         public Service1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         public void OnDebug()
  11.         {
  12.             OnStart(null);
  13.         }
  14.  
  15.         protected override void OnStart(string[] args)
  16.         {
  17.         }
  18.  
  19.         protected override void OnStop()
  20.         {
  21.         }
  22.     }
  23. }

Step 4: Add Task:

No we have to tell the service what to do when it start. To do this we have to define timer, and tell the Service when to run.
Time is base millisecond, so you have to be careful when you want to set timer.

Code Snippet
  1.  
  2. protected override void OnStart(string[] args)
  3. {
  4.     Timer timer = new Timer();
  5.     //This statement is used to set interval to 1 minute (= 60,000 milliseconds)
  6.     timer.Interval = (60 * 1000);
  7.     //handle Elapsed event
  8.     timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
  9.     //enabling the timer
  10.     timer.Enabled = true;
  11. }

Now have to add OnElapsedTime function. This function will contain your job. What ever you need to be done on start you have to add here.

Code Snippet
  1.  
  2. protected override void OnStart(string[] args)
  3. {
  4.     Timer timer = new Timer();
  5.     //This statement is used to set interval to 1 minute (= 60,000 milliseconds)
  6.     timer.Interval = (60 * 1000);
  7.     //handle Elapsed event
  8.     timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
  9.     //enabling the timer
  10.     timer.Enabled = true;
  11. }

no we have to add Code to OnStop function.

Code Snippet
  1. protected override void OnStop()
  2.         {
  3.             timer.Enabled = false;
  4.             System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
  5.         }

To perform testing you can create text file OnStart and OnStop and test your Service.

Step 5: Install Services

To install service you can use this command:

sc create ServiceName binPath= “C:\WinService\WindwosServiceTest.exe”

One comment

  1. Im very happy to find this web site. I want to to thank you for ones time just for this fantastic read!! I definitely appreciated every part of it and i also have you saved as a favorite to see new information in your blog.

Leave a Reply