C#
  Home arrow C# arrow 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 / 194
    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


    (Page 1 of 5 )

    Timer events can be very useful when you need to run a routine on a regular basis. In .NET, creating Windows services with the timer object is very easy to do. In this article we are going to create a timer which writes text to a file on regular intervals, and we’ll employ a Windows Service to control the timer.

    Timer Object Concept

    From the timer object (in the System.Timers namespace) we use the following properties and events:

    Elapsed: Everything in the timer evolves around the Elapsed event, which is the event that is raised every interval. You create code to be executed and call that code in the Elapsed event.

    Interval: Used to set the time between raising the Elapsed event.

    AutoReset: Ensures that the timer will be reset after every Elapse event. Therefore, if you would only like to execute the Elapse event once, you set the AutoReset property to false. When you omit the AutoReset property, it is assumed to be true.

    Enabled: Used to tell the timer to start or stop.

    Windows Service Concept

    A Windows Service has very defined start and stop events. Starting and stopping timers using these events is very organized and is run as a background process. If you define the Windows Service to start automatically, you need not worry about starting the timer again; this background process will keep on running until you stop the service and disable it. Since this is a background process, there will not be a user interface to dialog with the user. In case of exceptions, messages would be written to the Windows Event Log.

    Every Windows Service must have a Main method where you issue a Run command, which loads the service into the Services Control Manager. However, if you use Visual Studio.NET, all this code will be generated automatically.

    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...
       · It is really very good article explained in simple manner.
       · It's worked! Thanks!
       · it is exactly what I was looking for...though article doesn't have way to move back...
     

    C# ARTICLES

    - C# Meets Design Patterns
    - Coding a CRC-Generating Algorithm in C
    - 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





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek