C#
  Home arrow C# arrow Page 3 - Creating a Windows Service with C#, conclu...
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 
Moblin 
JMSL Numerical Library 
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#, concluded
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 37
    2006-03-14

    Table of Contents:
  • Creating a Windows Service with C#, concluded
  • Logging events
  • Creating the Controller
  • Using the SysTray Class

  • 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
     
     
    ADVERTISEMENT


    Creating a Windows Service with C#, concluded - Creating the Controller


    (Page 3 of 4 )

    Add a new Windows Application project to the TimeApp solution called “TimeServiceController”. You will see a form named “Form1” now. Change its Text property to “Time Service Controller”, and add the following label and buttons:

    Rename them “StatusLabel”, “StartButton”, and “StopButton”. Double-click on the Start button, click back into Design View, and double-click the Stop button. This adds in the click event handlers for our buttons. Now select the “Components” section of the toolbox and double-click “ServiceController”. Change the ServiceName property of the ServiceController to “TimeServerService”. We are done with Design View now; time to switch to Code View.

    Now that we have our buttons and service controller wired up, we just need to put a bit of code in the click handlers. Look how simple it is:

          private void StartButton_Click(object sender,
    System.EventArgs e)
          {
             this.serviceController1.Start();
             this.StatusLabel.Text = this.serviceController1.Status.ToString();
          }
     
          private void StopButton_Click(object sender,
    System.EventArgs e)
          {
             this.serviceController1.Stop();
             this.StatusLabel.Text = this.serviceController1.Status.ToString();
          }



    All we have to do now is set up a timer to poll the service status and update the status message in our controller. We will create the timer object in the constructor and call another method to check the status and update our label. The constructor should look like this:

          public Form1()
          {
             InitializeComponent();
     
             System.Timers.Timer t = new System.Timers.Timer(10000);
             t.Elapsed += new System.Timers.ElapsedEventHandler
    (UpdateStatus);
             t.Start();
     
             this.StatusLabel.Text =
    this.serviceController1.Status.ToString();
          }

    The UpdateStatus() method should look like this:

         private void UpdateStatus(object sender,
    System.Timers.ElapsedEventArgs e)
         {
            this.StatusLabel.Text = this.serviceController1.Status;
         }

    Set this class as your startup project and run it for a test drive. See a problem? Even with the timer in place, it seems that our label does not stay quite in sync. To fix this problem, we need to use the Service Controller’s WaitForStatus() method, like this:

          private void StartButton_Click(object sender,
    System.EventArgs e)
          {
             this.serviceController1.Start();
                       
             this.serviceController1.WaitForStatus(
                System.ServiceProcess.ServiceControllerStatus.Running);
     
             this.StatusLabel.Text =
    this.serviceController1.Status.ToString();
          }
     
          private void StopButton_Click(object sender,
    System.EventArgs e)
          {
             this.serviceController1.Stop();
     
             this.serviceController1.WaitForStatus(
                System.ServiceProcess.ServiceControllerStatus.Stopped);
     
             this.StatusLabel.Text =
    this.serviceController1.Status.ToString();
          }



    More C# Articles
    More By David Fells


       · Thanks for Reading!
       · Great set of articles, and very good timing as it is a good foundation for something...
       · the "iconlist" is missing and I had to remove it to compile it.How do I add...
       · i am new at C# and thus am having a hard time fixing the error.I think I...
       · this response should have been to the previous article. There appears to be a lot of...
       · First, I should note that this project was created with Visual Studio .NET 2003...
       · We have attached the full source code to all parts of the series. The author assures...
       · Download his code and you can see the part the article left out. Basically,...
     

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - 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...





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