In this tutorial, I explain how to create a Windows Service in C#. We will also learn how to configure a Windows Service in .NET and how to create a Windows Service in .NET using Visual Studio.
Windows Services are non UI software applications that run in the background. Windows services are usually started when an operating system boots and scheduled to run in the background to execute some tasks. Windows services can also be started automatically or manually. You can also manually pause, stop and restart Windows services.
Windows service is a computer program that runs in the background to execute some tasks. Some examples of Windows services are an auto update of Windows, check emails, print documents, SQL Server agent, file and folder scanning and indexing and so on. If you open your Task Manager and click on the Services tab, you will see hundreds of services running on your machine. You can also see the statuses of these services. Some services are running, some have paused, and some have stopped. You can start, stop, and pause a service from here by right click on the service.
We may also find all the services running on our machine in the following ways:
Open Visual Studio, go to File > New and select Project. Now select a new project from the Dialog box and select “Window Service” and click on OK button.
Go to Visual C# -> ”Windows Desktop” -> ”Windows Service” and give an appropriate name and then click OK
Once you click on OK button the below screen will appear, which is your service
Right click on the blank area and select “Add Installer”
Before you can run a Windows Service, you need to install the Installer, which registers it with the Service Control Manager.
After Adding Installer, ProjectInstaller will add in your project and ProjectInstakker.cs file will be open. Don’t forget to save everything (by pressing Ctrl + shift + s key)
Solution Explorer looks like this:
Right click on a blank area and select “View Code”
Very important: Don’t ever try to call any method before the call of the InitializeComponent method.
Select the InitializeComponent method and press F12 key to go definition
Now add the below line:
this.serviceInstaller1.Description = "My First Service demo"; this.serviceInstaller1.DisplayName = "MyFirstService.Demo";
In this step, we will implement a timer, and code to call the service at a given time. We will create a text file and write the current time in the text file using the service.
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; using System.Timers; namespace MyFirstService { public partial class Service1: ServiceBase { Timer timer = new Timer(); // name space(using System.Timers;) public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { WriteToFile("Service is started at " + DateTime.Now); timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); timer.Interval = 5000; //number in milisecinds timer.Enabled = true; } protected override void OnStop() { WriteToFile("Service is stopped at " + DateTime.Now); } private void OnElapsedTime(object source, ElapsedEventArgs e) { WriteToFile("Service is recall at " + DateTime.Now); } public void WriteToFile(string Message) { string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt"; if (!File.Exists(filepath)) { // Create a file to write to. using(StreamWriter sw = File.CreateText(filepath)) { sw.WriteLine(Message); } } else { using(StreamWriter sw = File.AppendText(filepath)) { sw.WriteLine(Message); } } } } }
Right click on your project or solution and select Rebuild.
Search “Command Prompt” and run as administrator
Fire the below command in the command prompt and press ENTER.
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
Now Go to your project source folder > bin > Debug and copy the full path of your Windows Service exe file.
InstallUtil.exe + Your copied path + \your service name + .exe
You may notice that the Windows service is running.
If you want to uninstall your service, fire the below command.
I hope, you found this tutorial easy to follow and understand.
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
View Comments
Good article and easy to understand for beginners
Thanks! Hope it helps others also.