DotNet Gallery



Login
Register
Contact Us
Privacy

Home .Net Article Articels .Net Article What is windows services? How ...

Dotnet GalleryWhat is windows services? How to create windows service?
C#.Net, Others
by mohan
Posted on 09/10/2008     Post .Net Article
Summary

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.


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.

It would 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

You can add sample code.


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

Thanks
Mohan.
Feedback about this Page
Rajkumar on 02/12/2008
Good job
keep posting.
Naveen kumar on 18/10/2008
Nice article
Keep posting like this useful articles. Thanks Kumar.
Submit feedback about this article
 
Copyright ©2008 Dot Net Gallery. All Rights Reserved.| Privacy Policy | Contact us