|
The core function of a Windows Service is to run an application in the background. One of the ways that it differs from an windows application in that a Windows Service starts before any user logs in to the system (if it has been setup to start at boot up), although it can also be setup in such a way that it requires user to start it manually. A Windows Service also has its own process hence it runs very efficiently. Normally a Windows Service will not have a user interface for a simple reason it can be run even if no one is logged on to the system but this is not a rule, you can still have a Windows Service with a user interface.
Creating a Windows Service:
Step 1:- Open VS 2005, Click on File > Projects.

Step 2:- This will open up a project and clicking on the hyperlink that is there on the page, you will be able to open the code behind page where you can code for the web service.
Step 3:- Add Functionality to the web service that you have created. There are two overridden function i.e. “OnStart” and “OnStop”. The OnStart function executes when you start your service and the OnStop function gets execute when you stop a service.
//OnStart
protected override void OnStart( string [] args)
{}
//OnStop
protected override void OnStop()
{}
Step 4:- Install and Run the Service: For Installation of the service on the computer, one need to have the administrator rights to install the service on the computer. The installUtil tool is provided by with the .Net framework to install the application.
Note:- The use should have administrator rights to install the utility. If the utility is not installed then it will display a popup when you run the program.

Example: Demonstrate Windows Services.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace winServiceFirst
{
public partial class OTestService : ServiceBase
{
public OTestService()
{
InitializeComponent();
}
protected override void OnStart( string [] args)
{
FileStream fs = new FileStream ( @"C:\windowServiceTest\trackerForWebService.txt" , FileMode .OpenOrCreate, FileAccess .Write);
StreamWriter m_streamWriter = new StreamWriter (fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin .End);
m_streamWriter.WriteLine( " ************************** \n" );
m_streamWriter.WriteLine( " mcWindowsService: Service Started \n" );
m_streamWriter.WriteLine( " ************************** \n" );
m_streamWriter.Flush();
m_streamWriter.Close();
}
protected override void OnStop()
{
FileStream fs = new FileStream ( @"C:\windowServiceTest\trackerForWebService.txt" , FileMode .OpenOrCreate, FileAccess .Write);
StreamWriter m_streamWriter = new StreamWriter (fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin .End);
m_streamWriter.WriteLine( " *************************** \n" );
m_streamWriter.WriteLine( " mcWindowsService: Service Stopped \n" );
m_streamWriter.WriteLine( " *************************** \n" );
m_streamWriter.Flush();
m_streamWriter.Close();
}
}
}
Output:
Note:- Thr program will write the content in the text file that we specified. The following lines gets printed in the text file.
**************************
mcWindowsService: Service Started
**************************
***************************
mcWindowsService: Service Stopped
*************************** |