Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  C# >  Creating a Windows Service with C#
Add to MyHood
   Creating a Windows Service with C#   [ printer friendly ]
Stats
  Rating: 4.5 out of 5 by 8 users
  Submitted: 03/01/02
Michael Manfre ()

 
  In this tutorial, I will go construct a simple windows service in C# that writes to a file whenever it is started or stopped.



   Create a new Windows Service C# Project titled "WindowsService1". Visual Studio.NET will create a project with a class that extends the
System.ServiceProcess.ServiceBase class. This class contains methods for controlling the process; OnStart(), OnStop(), OnPause(), and OnContinue(). The start and stop methods are required and the other are optional. Add using System.IO; and add the following code to the file:
System.IO.StreamWrite file;

protected override void OnStart(string[] args)
{
    // create or open the file. Default path is "C:\windows\System32\"
    file = new StreamWriter( new FileStream("ServiceTest.log", System.IO.FileMode.Append ) );
    this.file.WriteLine("Starting Service");
    this.file.Flush();
}

protected override void OnStop()
{
    this.file.WriteLine("Stopping Service");
    this.file.Flush();
    this.file.Close();
}




  Next you must create an installer class. The installer class sets the configuration for the Service and for the Service Processes. These specify the display name of the service and the account that will run the process. Create a new Installer class (see above image), add using System.ServiceProcess;, and the following code to the constructor:

ServiceInstaller si = new ServiceInstaller();
ServiceProcessInstaller spi = new ServiceProcessInstaller();

si.ServiceName = "Service1"; // this must match the ServiceName specified in WindowsService1.
si.DisplayName = "Devhood Tutorial Service"; // this will be displayed in the Services Manager.
this.Installers.Add(si);

spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem; // run under the system account.
spi.Password = null;
spi.Username = null;
this.Installers.Add(spi);


  The service must be installed before it can execute. Services are installed with "installutil.exe" and uninstalled with "installutil.exe /u" with the service executable as the last parameter. For example, "installutil.exe C:\project\WindowsService1\bin\WindowsService1.exe" will install the service and then the "Devhood Tutorial Service" to the Services Manager.



  After the service is installed, it can be started and stopped from the Services Section of Computer Management.

    The source files could be found here.

    Return to Browsing Tutorials

    Email this Tutorial to a Friend

    Rate this Content:  
    low quality  1 2 3 4 5  high quality

    Reader's Comments Post a Comment
     
    An interesting tutorial.
    -- Brian Simoneau, March 03, 2002
     
    Great job. Clear code samples and great illustrations.
    -- Michael S, March 03, 2002
     
    Very helpful. Works great!
    -- Kuniaki Tran, March 03, 2002
     
    This is a nice tutorial. It's simply done yet has all the visual aspects and clearly explains how to do everything.
    -- Victor Vuong, March 04, 2002
     
    On the first line of the first code sample, shouldn't:
    System.IO.StreamWrite file;
    instead be:
    System.IO.StreamWriter file;

    ?
    -- Andrew Petro, March 14, 2002
     
    Andrew, You are correct. The line should read "System.IO.StreamWriter file;". Thanks for pointing that out.
    -- Michael Manfre, March 15, 2002
     
    I found this tutorial very helpful, and I'm using some of the information provided in it right now. Also, thanks for providing the source code, it cleared up some questions I previously had.

    (5)***** Stars.
    -- Robin S, March 15, 2002
     
    If you can't see the images, this means that my cable is taking a crap again. I apologise for this and will move the images to another source when I get a chance.
    -- Michael Manfre, March 19, 2002
     
    Good clear tutorial
    -- Laurent Vauthrin, April 16, 2002
     
    Great article. Is-it possible to add a starting time to the service (every day at 10:00) ?

    Thanks for your help.
    -- farid farid, August 29, 2003
     
    Can you put the content on a server that is running? bit of a waste of time with no download and the images missing.

    Cheers,
    Stonie.
    -- Andrew Stone, November 14, 2003
     
    When i install the service and try to start it, it always gives me an error ->
    ---------------------------
    Services
    ---------------------------
    Could not start the Service1 is my .Net service service on Local Computer.

    Error 1053: The service did not respond to the start or control request in a timely fashion.

    ---------------------------
    OK
    ---------------------------
    -- Lalit P, November 27, 2003
     
    Copyright © 2001 DevHood® All Rights Reserved