Click here to Skip to main content
Licence 
First Posted 28 Oct 2003
Views 55,553
Bookmarked 17 times

A CountDownTimer inherited from System.Windows.Forms.Timer

By | 28 Oct 2003 | Article
A timer that will only run the number of times you specifed.

Sample Image - CountDownTimer.jpg

Introduction

I was once in need of a timer that will only run a specified number of times. I also wanted to know when the timer was done/stopped. Than I started to write a new class inherited from the existing System.Windows.Forms.Timer.

I have added an example application that utilizes the CountDownTimer. Initially it runs with 20 times. But you can change the value with the NumericUpDown control on top, in the middle. The reason why I have 2 Start/Stop buttons is, I used different methods for Start()/Stop() and Enabled=true/false before. But now it's actually the same thing. It is also possible to set the timer to sleep by pushing the Sleep button, just push it again to wake the timer up again.

Timer class basics

I defined 2 new events Started and Stopped. Especially Stopped is quite useful. To be able to count down, we have to subscribe to the Tick event from the Timer base class in the constructor. Every time the event will be fired, I call the function countdown and it will decrement the currentDownCounter variable until it reaches 0. Then the timer will stop, that's it. I also introduced a new property Sleep, which actually stop the base Timer if set to TRUE and starts it again if set to FALSE. But only if the Timer is not already stopped.

The code

public class CountDownTimer : System.Windows.Forms.Timer
 {
  public event EventHandler Started; // event that occurs when Timer started
  public event EventHandler Stopped; // event that occurs when Timer stopped
  // variable will keep information about initial count down value
  private int downCounter = -1;
  // will keep the current count down value
  private int currentDownCounter = 0; 
  // indicates if timer is stopped, helper variable for sleep mode
  private bool stopped = false;
  public int _countDown    // property, gives access to downCounter
  {
   get
   {
    return this.downCounter;
   }
   set
   {
    this.downCounter = value;
   }
  }
  public int _currentCountDown // property, gives access to currentCountDown
  {
   get
   {
    return this.currentDownCounter;
   }
   set
   {
    this.currentDownCounter = value;
   }
  }
  // default constructor
  public CountDownTimer()
  {
   
  }
  // constructor to initialize the countdown timer
        public CountDownTimer(int countDown)
  {
   this.downCounter = countDown;
   this.currentDownCounter = countDown;
   // subscribe to the Tick event of the Timer base class,
   // to be able to count down
   base.Tick += new System.EventHandler(this.countdown); 
  }
  
  // will be called when Timer started
  protected virtual void OnStarted(EventArgs e) 
  {
   if (Started != null) 
   {
    //Invokes the delegates.
    Started(this, e); 
   }
  }
  // will be called when Timer stopped
  protected virtual void OnStopped(EventArgs e) 
  {
   if (Stopped != null) 
   {
    //Invokes the delegates.
    Stopped(this, e); 
   }
  }  
  // will start the timer, overwrites the base Start method
  public new void Start()  
  {   
   this.Enabled = true;
  }
  // will stop the timer, overwrites the base Stop method
  public new void Stop()  
  {
   this.Enabled = false;
  }
  // will set the countDownTimer to sleep mode
  public bool Sleep 
  {
   set
   {
    // only set, if timer not stopped; will just stop the base timer
    if (!this.stopped) base.Enabled = !value;
   }
   get
   {
    return !base.Enabled;
   }
  }
  // overwrites the base Enabled property
  public override bool Enabled 
  {
   get
   {
    return base.Enabled;
   }
   set
   {
    base.Enabled = value;
                
    if (value)
    {
     this.stopped = false;
     this.currentDownCounter = this.downCounter;
     OnStarted(new System.EventArgs());
    }
    else
    {
     this.stopped = true;
     OnStopped(new System.EventArgs());
    }
   }
  }
  // will be called if base class fires a Tick event,
  // counts down and stops if 0 reached
  private void countdown(object sender, System.EventArgs e) 
  {
   if (this.downCounter == -1)
   {
    // run forever
    return;
   }
   else if (this.currentDownCounter > 0)
   {
    this.currentDownCounter--;
   }
   
   if (this.currentDownCounter == 0)
   {
    this.Enabled = false;
   }
  }
 }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sven So.

Software Developer (Senior)
biggest furniture dealer in the world
Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberCanggihsatriyo14:45 8 Mar '12  
Generalnice Pinmemberthe_code_master1:33 14 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 29 Oct 2003
Article Copyright 2003 by Sven So.
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid