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.
This is how your project will look like after you create:
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.
Step 3: Work In DEBUG mode:
If you run the project now, you will get windows service start failure.
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:
Now open the Service1.cs and Add the following code:
your Service1.cs should look like this now:
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.
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.
no we have to add Code to OnStop function.
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”