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.
After you successfully add the installer your project will look like this:
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:
- namespace WindwosServiceTest
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static void Main()
- {
- #if DEBUG
- Service1 myService = new Service1();
- myService.OnDebug();
- System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
- #else
- ServiceBase[] ServicesToRun;
- ServicesToRun = new ServiceBase[]
- {
- new Service1()
- };
- ServiceBase.Run(ServicesToRun);
- #endif
- }
- }
- }
Now open the Service1.cs and Add the following code:
- public void OnDebug()
- {
- OnStart(null);
- }
your Service1.cs should look like this now:
- namespace WindwosServiceTest
- {
- public partial class Service1 : ServiceBase
- {
- public Service1()
- {
- InitializeComponent();
- }
- public void OnDebug()
- {
- OnStart(null);
- }
- protected override void OnStart(string[] args)
- {
- }
- protected override void OnStop()
- {
- }
- }
- }
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.
- protected override void OnStart(string[] args)
- {
- Timer timer = new Timer();
- //This statement is used to set interval to 1 minute (= 60,000 milliseconds)
- timer.Interval = (60 * 1000);
- //handle Elapsed event
- timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
- //enabling the timer
- timer.Enabled = true;
- }
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.
- protected override void OnStart(string[] args)
- {
- Timer timer = new Timer();
- //This statement is used to set interval to 1 minute (= 60,000 milliseconds)
- timer.Interval = (60 * 1000);
- //handle Elapsed event
- timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
- //enabling the timer
- timer.Enabled = true;
- }
no we have to add Code to OnStop function.
- protected override void OnStop()
- {
- timer.Enabled = false;
- System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
- }
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”
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.