Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#
Article

A CountDownTimer inherited from System.Windows.Forms.Timer

Rate me:
Please Sign up or sign in to vote.
3.75/5 (8 votes)
28 Oct 2003CPOL1 min read 74.5K   1.4K   19   2
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

C#
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
C#
// variable will keep information about initial count down value
private int downCounter = -1;
// will keep the current count down value
private int currentDownCounter = 0;
C#
// indicates if timer is stopped, helper variable for sleep mode
private bool stopped = false;
C#
public int _countDown    // property, gives access to downCounter
{
 get
 {
  return this.downCounter;
 }
 set
 {
  this.downCounter = value;
 }
}
C#
public int _currentCountDown // property, gives access to currentCountDown
{
 get
 {
  return this.currentDownCounter;
 }
 set
 {
  this.currentDownCounter = value;
 }
}
C#
// default constructor
public CountDownTimer()
{

}
C#
// 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);
 }
}
C#
// will be called when Timer stopped
protected virtual void OnStopped(EventArgs e)
{
 if (Stopped != null)
 {
  //Invokes the delegates.
  Stopped(this, e);
 }
}
C#
// will start the timer, overwrites the base Start method
public new void Start()
{
 this.Enabled = true;
}
C#
// will stop the timer, overwrites the base Stop method
public new void Stop()
{
 this.Enabled = false;
}
C#
// 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;
 }
}
C#
// 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());
  }
 }
}
C#
 // 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) a large company
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Canggihsatriyo8-Mar-12 14:45
Canggihsatriyo8-Mar-12 14:45 
Generalnice Pin
the_code_master14-Sep-06 1:33
the_code_master14-Sep-06 1:33 

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

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