C#
  Home arrow C# arrow Page 3 - Creating a Windows Service with C#, contin...
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 
Mobile Linux 
App Generation ROI 
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#, continued
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2006-03-07

    Table of Contents:
  • Creating a Windows Service with C#, continued
  • Creating the Windows Service Project
  • Coding the Service
  • Installing the Service

  • 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#, continued - Coding the Service


    (Page 3 of 4 )

    Now we need to do something about our control loop, since we can’t use console input anymore to kill the program. We also need to decide what type of event logging we are going to do.

    To control our loop, we need to add a property to the class; we will call it “isStopped.” We will have to add a couple of lines to the OnStart() and OnStop() methods. Add this to the properties section of the class:

    private bool isStopped = false;

    The new OnStart(), OnStop(), and Start() methods are shown below:

           protected override void OnStart(string[] args)
           {
             this.isStopped = false;
             this.Start();
           }
     
           protected override void OnStop()
           {
              this.isStopped = true;
           }
     
     
          public void Start()
          {
             this.listener.Start();
     
             Socket s;
             Byte[] incomingBuffer;
             Byte[] time;
             int bytesRead;
     
             while (!this.isStopped)
             {
                s = this.listener.AcceptSocket();
                incomingBuffer = new Byte[100];
                bytesRead = s.Receive(incomingBuffer);
     
                time = Encoding.ASCII.GetBytes(
                   System.DateTime.Now.ToString().ToCharArray());
                s.Send(time);
             }
     
             this.listener.Stop();
          }

    Now the server will start and stop using the built in Windows Service hooks.

    More C# Articles
    More By David Fells


       · Thanks for reading!
       · I read this article as I was doing a similar thing to make a Quote of the day...
       · Hi,I followed this article in creating the windows service but it hangs. I...
       · I'm seeing the same problem, has any come up with a fix? It will not shutdown until...
       · Now I am not certain what the exact way to do this is in C#/.NET, however in Java...
       · Pls Could you tell me how can I use .Net 2005 with this project. I can,t find some...
       · Close the socket from the OnStop and any waiting operation on that socket will...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - 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...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT