C#
  Home arrow C# arrow Page 4 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 43
    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 - Using the SysTray Class


    (Page 4 of 4 )

    The last thing on our list is to send our TimeServiceController program to the system tray when it is minimized. We will also want to show a balloon with a message if the service status changes unexpectedly. Create a new class file called SysTray.cs in the TimeServiceController project and paste in the code from vbAccelerator.com: http://www.vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects/
    SysTray/SysTray_Code_zip_SysTrayTester_SysTray_cs.asp
    .

    Add the vbAccelerator.Components.Shell namespace to your Form1 class file:

    using vbAccelerator.Components.Shell;

    Add a SysTray object as a class field:

    private SysTray sysTray;

    Initialize the SysTray object at the end of the constructor:

    this.sysTray = new SysTray(this);
    iconList.Images.Add(new Icon("../../App.ico"));
    this.sysTray.IconImageList = iconList;
    this.sysTray.IconIndex = 0;

    Now we need to wire up our form to keep track of when it has been minimized so we can hide it and add it to the task bar. Add this line to the end of InitializeComponent() method:

    this.SizeChanged += new System.EventHandler(DoResize);

    The Minimize() method looks like this:

          private void DoResize(object sender, System.EventArgs e)
          {
             if (this.WindowState == FormWindowState.Minimized)
             {
                this.sysTray.ShowInSysTray = true;
                this.Hide();
             }
          }

    If you run the project now you will see that when you minimize, it does indeed show up on your system tray and the main window vanishes from the taskbar altogether-–but clicking on the system tray icon does not restore it. That’s because we haven’t created an event handler for the sysTray.DoubleClick event. Add this line in the constructor:

    this.sysTray.DoubleClick += new EventHandler(DoRestore);

    And add this method:

          private void DoRestore(object sender, EventArgs e)
          {
             this.sysTray.ShowInSysTray = false;
     
             if (!this.Visible)
             {
                this.Visible = true;
             }
             if (this.WindowState == FormWindowState.Minimized)
             {
                this.WindowState = FormWindowState.Normal;
             }
             this.BringToFront();
          }

    Now when you run your controller application you can minimize it to the system tray and double click the system tray icon to restore it. The last thing we need to do is add a bit of code to our timed method UpdateStatus() to check for an unexpected service status and show a balloon tooltip. Since we will be stopping and starting the service with the buttons in our application, any service status change, when minimized, should warrant a popup balloon. Thanks to vbAccelerator.com’s SysTray class, this is pretty simple:

         private void UpdateStatus(object sender,
    System.Timers.ElapsedEventArgs e)
         {
            string status = this.serviceController1.Status.ToString
    ();
            
            if (this.Visible == false)
               this.sysTray.ShowBalloonTip(status);
     
            this.StatusLabel.Text = status;
         }

    To test this tooltip out, run the application and minimize it. Then go to Computer Management -> Services and stop the TimeServerService. You will see a balloon popup notification within ten seconds!

    The End

    All done! We’ve done quite a bit in these past three articles and I hope you learned something you didn’t already know. We have created a basic TCP server and installed it as a Windows Service with configurable logging and a small application to stop and start the service that runs minimized on the system tray and notifies us of services status changes. You can use this service as a foundation for a lot of interesting stuff, and I encourage you to experiment with building a more advanced server and client and to add more complex features to the service controller.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT