Creating a Windows Service with C#, introduction
(Page 1 of 4 )
Windows Services handle a variety of tasks for your computer, usually by running quietly in the background. In this article, you will learn how to create and install a simple Windows Service. This is the first of three parts.
The Zip file containing the source code for this and the other parts of this series can be found HERE.
Windows Services are applications that run in the background and perform various tasks. At any moment, your Windows PC will likely have no less than twenty running services, handling everything from TCP/IP to task scheduling. Since Windows Services are generally “always on,” they provide the perfect platform for applications that require persistence but do not require user interaction. Services often come with simple applications that allow you to stop, start, or pause them. Most servers, such as IIS, SQL Server, and Apache run as services.
This article will teach you how to create and install a simple Windows Service, complete with a simple Windows Form application to stop and start the service. We are going to create a simple server that accepts incoming requests on a certain TCP port, just like a web or email server. Our server will answer all requests with the current date and time string.
First we will build our server as a console application so we can debug it without having to constantly uninstall/reinstall a service. Once we have the console application built, we will create the Windows service and get it installed. Finally, we will create a service controller application that lets us stop and start the server, and register it to the system tray on the taskbar. These tasks will be seperated into three separate articles.
Creating the Console Application
Open up Visual Studio .NET (I’m using the 2003 edition) and select “New Project.” Select “Visual C#” projects on the left, then scroll down to Console Application on the right. Enter the name “TimeApp” for your project in the box below. Click “OK” and wait for the project workspace to load up.
The first thing we want to do is rename Class1 to something more meaningful. Let’s rename it “TimeServer.” To rename the file, right click the file in the solution explorer and select rename. Change the file name property to “TimeServer.cs”. In the class file itself, simply change “class Class1” to “class TimeServer.” We also need to add a few namespaces to our class. We need to add System.Net and System.Net.Sockets for our network functionality, and System.Text for text encoding. Once you’ve added those namespaces, your class file should look like this:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace TimeApp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class TimeServer
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}
}
}
Now it’s time to start writing some code!
Next: Creating a Listener >>
More C# Articles
More By David Fells