65.9K
CodeProject 正在变化。 阅读更多。
Home

从 System.Windows.Forms.Timer 继承的倒计时器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.75/5 (8投票s)

2003年10月29日

CPOL

1分钟阅读

viewsIcon

75477

downloadIcon

1390

一个只会运行你指定次数的计时器。

Sample Image - CountDownTimer.jpg

引言

我曾经需要一个只会运行指定次数的计时器。我还想知道计时器何时完成/停止。于是我开始编写一个新的类,继承自现有的 System.Windows.Forms.Timer

我添加了一个示例应用程序,该应用程序使用了 CountDownTimer。最初它运行 20 次。但是你可以使用顶部的 NumericUpDown 控件更改该值,位于中间。我之所以有 2 个开始/停止按钮,是因为我之前使用了不同的方法来处理 Start()/Stop()Enabled=true/false。但现在实际上是一样的。也可以通过按下休眠按钮将计时器设置为休眠状态,再次按下即可唤醒计时器。

计时器类基础

我定义了 2 个新事件 StartedStopped。特别是 Stopped 非常有用。为了能够倒计时,我们必须在构造函数中订阅 Timer 基类中的 Tick 事件。每次触发该事件时,我都会调用 countdown 函数,它会递减 currentDownCounter 变量,直到达到 0。然后计时器将停止,就完成了。我还引入了一个新的属性 Sleep,它实际上会在设置为 TRUE 时停止基础 Timer,并在设置为 FALSE 时再次启动它。但前提是 Timer 尚未停止。

代码

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;
   }
  }
 }
© . All rights reserved.