Windows Service is a standalone application which is started automatically or manually as per setting and complete their specific task. We can handle service using start, restart and stop. It does not have any User Interface from where you can handle the service feature. It is created and hosted on the system.
So, we can say basically Windows Service is an application that run in background and work on various tasks. Some time it is started automatically when system boot and sometimes you need to start the service manually. All the Windows Service is controlled through the Service Control Manager.
Create the Windows Service, Open Visual Studio, from the File Menu, select New and then choose Project. It will open a New Project window where you can choose various type of the project.
Go to Visual C# and select Classic Desktop and from the next window, choose Windows Service. Name the service “MyFirstWindowsService” and click to OK.
It will add a Windows Service for you. Rename the Service1.cs to TestService.cs.
See the following Program.cs code, it is responsible for running the Windows Service.
Open TestService.cs [Design] mode and add a Timer control.
Open TestService.cs and made the changes in the code as in the following. You need to write the logic to process some task on OnStart and OnStop.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- using System.Threading.Tasks;
- namespace MyFirstWindowsService
- {
- public partial class TestService: ServiceBase
- {
- System.Timers.Timer timeDelay;
- int count;
- public TestService()
- {
- InitializeComponent();
- timeDelay = new System.Timers.Timer();
- timeDelay.Elapsed += new System.Timers.ElapsedEventHandler(WorkProcess);
- }
- public void WorkProcess(object sender, System.Timers.ElapsedEventArgs e)
- {
- string process = "Timer Tick " + count;
- LogService(process);
- count++;
- }
- protected override void OnStart(string[] args)
- {
- LogService("Service is Started");
- timeDelay.Enabled = true;
- }
- protected override void OnStop()
- {
- LogService("Service Stoped");
- timeDelay.Enabled = false;
- }
- private void LogService(string content)
- {
- FileStream fs = new FileStream(@ "d:\TestServiceLog.txt", FileMode.OpenOrCreate, FileAccess.Write);
- StreamWriter sw = new StreamWriter(fs);
- sw.BaseStream.Seek(0, SeekOrigin.End);
- sw.WriteLine(content);
- sw.Flush();
- sw.Close();
- }
- }
- }
Here in above code, you can see, service will start on OnStart and stop on OnStop method. You need to create logging system for logging the task to be completed in the process.
Now it is time to
Add Installer to install the service. Go to
TestService.cs Design View and right click on the screen and choose
Add Installer. And provide the name as “
ProjectInstaller”.
When you click on
Add Installer, it will automatically add the
ProjectInstaller.cs with two new components “
ServiceProcessInstaller1” and “
ServiceInstaller1”.
Go to property of the
ServiceProcessInstaller1 and change the Account type as “
Local System”.
Now go to
ServiceInstaller1 and choose the property option for this and change the
ServieName for the service from the property.
Now build the project and go to the build location where you can find the .exe for the windows service.
So, we can install it.
For installing the Windows Service, Open Visual Studio Command Prompt as Administrator mode and go to the following location where your windows service has created service .exe file and here you can use
InstallUtil.exe “MyFirstWindowsService.exe” for installing the service.
To see the installing service, go to program and type
Services.msc in search program. Here you can find your service “
My First Windows Service” and in the left hand side you can see the
Start menu from where you can start your service.
While starting the service, you will see two more options “
Stop” and “
Restart”.
Open the
D:\TestServiceLog.txt to see the log process of the Windows Service.
When you click to stop the service, in the log file you can see the service has been stopped.
Thanks for reading this article, hope you enjoyed. It.