Home > Articles > Windows Services Articles > What is windows service? How to create windows service using .Net?

What is windows service? How to create windows service using .Net?

by Thamil   on Nov 14, 2010   Category: Windows Services  | Level: Beginner  |  Views: 3478    |  Points: 100   
Like this article? Bookmark and Share:
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.

Introduction


Windows service automatically start when the machine boot and also manually we can start and 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 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

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;

namespace TestWinService

{

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 SecondUserService()};

//

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 Onstart function executes when you start your services, Onstop executes when stop your services

Step 3: Install and Run the Service

You will get windows service exe, when you  build the windows service and we need need to register the service using installutil in command line.

installutil D:\TestWinService \bin\Debug\TestService.exe

Step 4: Uninstall the Service

we need to use /u option to uninstall the service.

installutil /u D:\TestWinService \bin\Debug\TestService.exe

Step 5: Stop and Start

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

Timer

Using timer events, we can run the service on a regular basis.



Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts