What is windows service? How to create windows service using C#.net?

Parasu
Posted by in Others category on for Beginner level | Views : 33307 red flag
Rating: 3.5 out of 5  
 2 vote(s)

The main function of windows service is to run the application in the background. Mainly creating service in long running executable applications, in earlier times we called as NT services.

 

Windows service automatically start when the machine boot and also manually start, stop. Usually windows service will not have user interface. In windows xp or some other windows os you can view the list of Services currently running on your compure.

Go to Control Panel -> Administrative Tools -> Services 

Step 1. Create the Windows Service Project


To create a new Window Service, select Windows Service option from your Visual C# New Projects,
The Wizard include the WebService1.cs class to your project.The default code of WebService1.cs added by the Wizard looks like here

namespace TestWinService 
{
using System;
using System.Collections;
using System.Core;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Web.Services;
using System.Diagnostics;
using System.ServiceProcess;
public class WinService1 : System.ServiceProcess.ServiceBase
{
/// Required designer variable.
private System.ComponentModel.Container components;
public WinService1()
{
// This call is required by the WinForms Component Designer. InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
/ create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new WinService1(), new
ySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "WinService1";
}
/// Set things in motion so your service can do its work.
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
}
/// Stop this service.
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}


Step 2. Add your code in Onstart and Onstop function.

The Onstop function executes when you start your services, Onstop executes when stop your services

Step 3: Install and Run the Service

Build of the windows service, you can get exe and you need to register service using installutil in command line.
installutil D:\TestWinService \bin\Debug\TestService.exe

Uninstall the Service

You can use /u option to uninstall the service.
installutil /u D:\TestWinService \bin\Debug\TestService.exe

Go to Administrative Tools --> Computer management --> Under Services and Applications you can see --> TestService.exe. Right click on the TestService.exe with the properties you can stop and start the services.

Timer

Otherwise you can use Timer events when you need to run a routine on a regular basis


 

Page copy protected against web site content infringement by Copyscape

About the Author

Parasu
Full Name: Thamil selvan J.
Member Level:
Member Status: Member
Member Since: 2/12/2009 4:04:58 AM
Country: India



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)