Click here to Skip to main content
Click here to Skip to main content

A CountDownTimer inherited from System.Windows.Forms.Timer

, 28 Oct 2003
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sven So.
Software Developer (Senior) biggest furniture dealer in the world
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberCanggihsatriyo8-Mar-12 14:45 
Generalnice Pinmemberthe_code_master14-Sep-06 1:33 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 29 Oct 2003
Article Copyright 2003 by Sven So.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid