C#
  Home arrow C# arrow Page 2 - 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 
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#, 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 - Logging events


    (Page 2 of 4 )

    With the helper methods in place, actually logging events is a matter of calling Log(message). Typically you would put messages in OnStart() and OnStop() indicating service start and stop, and in this case you may want to log every time someone connects to the server. Add this line to OnStart():

    this.Log(this.ServiceName + " Started");

    And add this line to OnStop():

    this.Log(this.ServiceName + " Stopped");

    In the Start() method, inside the control loop, we need to check the number of bytes read to see if a request has been received and, if one has, to make a note of it. Add this line to the Start() method after the line reading “s.Send(time);”:

    this.Log("Received request: " + bytesRead.ToString());

    You have probably noticed that there are no try/catch blocks in this service. It would probably be wise to add one in our control loop! Wrap everything inside the while loop in a try statement, then in the catch, write to the event log, like this:

             while (!this.isStopped)
             {
                try
                {
                   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.Log("Received request: " + bytesRead.ToString
    ());
                }
                catch (Exception ex)
                {
                   this.Log(ex);
                }
             }



    Now that logging is complete, let’s move on to creating our controller application. Be sure you reinstall the service first to activate the changes we have made.

    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

    - 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 2 hosted by Hostway
    Stay green...Green IT