C#
  Home arrow C# arrow Creating a Windows Service with C#, introd...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Creating a Windows Service with C#, introduction
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 48
    2006-02-28

    Table of Contents:
  • Creating a Windows Service with C#, introduction
  • Creating a Listener
  • Making the Listener Listen
  • Testing the Server

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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!

    More C# Articles
    More By David Fells


       · Thanks for reading!
       · Hi ,i have a C# console application which sends and receives messages from...
     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    IBM developerWorks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway