C#
  Home arrow C# arrow Page 4 - Timer Objects in Windows Services with C#....
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#

Timer Objects in Windows Services with C#.NET
By: Rogier Doekes
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 185
    2004-10-18

    Table of Contents:
  • Timer Objects in Windows Services with C#.NET
  • Setting up the Project
  • Adding Code
  • Coding the Windows Service Start and Stop Event Handlers
  • Building and 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


    Timer Objects in Windows Services with C#.NET - Coding the Windows Service Start and Stop Event Handlers


    (Page 4 of 5 )

    In the Service1.cs file we only need to write code for the OnStart and OnStop events. In the OnStart() void method, add the following line of code:

    AddToFile(“Starting Service”);

    Now, in the OnStop event add the following line:

    AddToFile(“Stopping Service”);

    This is all we need to do to have a working Windows Service. Next we’ll add the timer to the service.

    Creating the Timer

    Just under the class definition in the Service1.cs file, add the following global variables:

    //Initialize the timer
    Timer timer = new Timer();

    The idea behind the timer is that it sleeps for a specified period (defined by the interval method), and then executes the code specified with the elapsed event. We need to define a method that will be executed when the Elapsed event occurs, and we do this with the following code, which adds a line of text to the file:

    Private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
         AddToFile(“ Another entry”);
    }

    Now we can setup the timer.

    In the OnStart method we add code to reflect what to do when the elapsed event is raised. In this case, we need to invoke the OnElapsedTime method we defined above, set the interval (in milliseconds) the project needs to sleep, and enable the timer so it raises the Elapsed event.

    The complete OnStart method looks like this:

    protected override void OnStart(string[] args)

    {

         //add line to the file
         AddToFile(“starting service”);

    //ad 1: handle Elapsed event
    timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

    //ad 2: set interval to 1 minute (= 60,000 milliseconds)

    timer.Interval = 60000;

    //ad 3: enabling the timer
    timer.Enabled = true;
    }

    The OnStop event also needs to be modified. A mere timer.Enabled = false suffices. The complete OnStop method looks like this:

    protected override void OnStop()
    {
         timer.Enabled = false;
         AddToFile(“stopping service”);
    }

    That’s all the coding we need to do!

    More C# Articles
    More By Rogier Doekes


       · Thanks! Your article was quite helpful, however i couldn't find the "add installer"...
       · Thanks this is the best and simplest article on the usage of timers
       · You can find the AddInstaller option by right click on the properties window where ...
       · This was the best and simple example which I used it to make my first Service....
       · hi, Thanks for your Article. This one is so good to know about the windows service...
       · Heya! This really helped me, as well! Thing is, I got it all to compile & work,...
       · This really helped me a lot to know what the windows services are.
       · Have the same problem. Does anybody knows what the problem is?
       · Your article is very helpful.Hovever, please note that the exe file has been...
       · 1. Manually create the text file (don't trust the program to do it)2. Right click...
       · Go to Design page and right click on disign page u will see the option add...
       · God bless you for posting this neat example. Thanks a lot !!
       · Could not start Timer Service service on local computer.Error 5: access...
       · Is your property serviceProcessInstaller1>Account set to 'LocalSystem'? If set to...
       · Hello, I get the same problem. I am unable to start the service, it tells me that...
       · Thank You Very Much...I went through so many articles before this, But I was not...
       · There's a good explanation about how to add the installer and get the service...
     

    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 4 Hosted by Hostway
    Stay green...Green IT