Creating a Windows Service Using C#

Windows Services is the most important feature for the long standing applications to run services automatically soon after the system boots.We can manually control these Services by starting and stoping at a specific time.

Introduction

Windows Services are the most important feature for the long standing applications to run services automatically soon after the system boots.We can manually control these Services by starting and stoping at a specific time.Generally we can run these services on the server without any interference with the other users in the system.Here we can even restrict some specific services to be available only for specific users.
In this article iam going to explain -
  1. How to create a Windows Service?
  2. How to automatically populate the information about the service(StartTime,EndTime) in a text file?
I will divide this entire article into a step by step process with screen shots for better understanding purpose.

Step 1:

Open Visual Studio(My version 2008) and add a windows service project in this way

Step 2: 

Now open Service1.cs[Design] and have a right click you get view code option, select that option. So you will be getting Service1.cs file . Add this code to that file
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.Text;

using System.IO;

using System.Threading;

namespace WindowsService1

{

   public partial class Service1 : ServiceBase

   {

public Service1()
{
   InitializeComponent();
}
static void run()
{
//while (true)
{StreamWriter str = new StreamWriter("D:\\ContinuosLoop.txt", true);
str.WriteLine("Visit DotNetFunda Regularly");
str.Close();
}
}
Thread thr = new Thread(new ThreadStart(run));
protected override void OnStart(string[] args)
{
StreamWriter str = new StreamWriter("D:\\Log.txt", true);
str.WriteLine("Service started on : " + DateTime.Now.ToString());
str.Close();
thr.Start();
}
protected override void OnStop()
{
StreamWriter str = new StreamWriter("D:\\Log.txt", true);
str.WriteLine("Service stoped on : " + DateTime.Now.ToString());
str.Close();
thr.Abort();
}
}
}

 

Step 3:

We cannot directly run the service what we need to be done is add an installer. For that go back to Service1.cs[Design] file and right click and select AddInstaller option in this way

Now the Screen looks like this with 2 options this is ProjectInstaller.cs[Design]


 

Step 4:

Now we have 2 controls namely
      1) ServiceProcessInstaller1
            For this we have to change the Account Property to LocalSytem        
      2) ServiceInstaller1
For this we have to change the start type to manual and make sure you service name is well remembered.I gave ~~~SriService~~~ as in my example.
With this the coding part is completed now build the solution which will create the necessary files in bin folder.

Step 5:

As you all know inorder to run this service we have to install this service for that open VisualStudio Command Prompt. Go that the project path which was created by you untill the Project/bin/debug folder in this way

   Inorder to install we have to type installutil -i WindowsService1.exe at that path which looks like this

Step 6:

Now after successful installation you will recieve an message stating that "The Transacted install has completed" . If you get this that means your installation is successful now you can go and wacth the services folder to that go to control panel/perforamanceMaintainance/AdministartiveTools/Services.
At that palce you could see your service in this way

 

Step 7:

Now start the service. As per our coding after starting the service we create 2 text documents in the D: drive as per my example. Those are ContinousLoop.txt and Log.txt
In Log.txt the time and date of the service started and stopped will be displayed.
If you could observe i commented a line with while (true) in Service1.cs file. If we uncomment it, the line "Visit DotNetFunda Regularly" prints continuosly untill and unless we explicitly stop the service. so as i uncommented it that line will be displayed only once.
The final text documents will look like this

Conclusion

By this way we can create a Windows Service and can work with it accordingly. Hope I provided useful information in this article. Please provide your comment to imrpove this article if you feel so. Thanks!

Like