C#winform-在Timer_tick事件中引发个人事件



我创建了一个包含计时器的小类CountDown。

类非常简单:接收时间目标,并用计时器启动倒计时。

当目标到达时,我的个人事件开火:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CountDownWithEvent
{
public delegate void countDownFinishEventHandler(Object sender, EventArgs e);
class CountDown
{
private DateTime _target;
private System.Windows.Forms.Timer _timer;
System.TimeSpan _timeMissing;

public event countDownFinishEventHandler CountDownFinish;
public CountDown(DateTime targetTime)
{
this._target = targetTime;
this._timer = new System.Windows.Forms.Timer();
this._timeMissing = new TimeSpan();
}
public DateTime TargetTime
{
get;
}
public TimeSpan timeMissing
{
get;
}
public void CountDownStart()
{
_timer.Interval = 1000;
_timer.Tick += new EventHandler(timer_tick);
_timer.Start();
}
protected virtual void timer_tick(Object sender, EventArgs e)
{
//if (_timer.Tick != null)
//{
//}
System.DateTime now = System.DateTime.Now;
_timeMissing = _target.Subtract(now);
if (!(timeMissing.TotalSeconds > 0))
{
_timer.Stop();
if(CountDownFinish != null)
{
EventArgs b = new EventArgs();
CountDownFinish(this, b);
}
}
}

}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CountDownWithEvent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CountDown CountDown = new CountDown(new DateTime(2016, 05, 27, 14, 48, 00));
CountDown.CountDownFinish += new countDownFinishEventHandler(onCountDown);
CountDown.CountDownStart();
}
private void onCountDown(Object sender, EventArgs e)
{
MessageBox.Show("time expired! ");
}
}
}

我使用EventArgs,而不是编写派生类,因为我不需要任何事件的特殊信息(要理解,参数e)

现在我的处境有点不同寻常:

protected virtual void timer_tick(Object sender, EventArgs e)
{
//if (_timer.Tick != null)
//{
//}
System.DateTime now = System.DateTime.Now;
_timeMissing = _target.Subtract(now);
if (!(timeMissing.TotalSeconds > 0))
{
_timer.Stop();
if(CountDownFinish != null)
{
EventArgs b = new EventArgs();
CountDownFinish(this, b);
}
}
}

当我调用CountdownFinish(this,e);时,e参数引用timer_tick通过EventArgs计时器不一致所以我不知道该怎么做???

事实上,我已经实例化了新的EventArgs b

EventArgs b = new EventArgs();
CountDownFinish(this, b);

但我不知道这是否是正确的路径

现在我是另一个问题:

我想在标签上看到离进球还有多少时间。并将其刷新到任意时间。而我想把程序图的定时器逻辑分开。。我该怎么做

非常感谢您的理解和帮助!(抱歉英语不好,不是我的语言吗:)

@Reza Aghaei

好的,这是我的解决方案。。你觉得怎么样?!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EventsTry
{
public delegate void countDownFinishEventHandler(Object sender, EventArgs e);
public delegate void TimeLeftChangedEventHandler(Object sender, TimeLeftDateEventArgs e);
class CountDown
{
private DateTime _target;
private System.Windows.Forms.Timer _timer;
System.TimeSpan _timeMissing;
public event countDownFinishEventHandler CountDownFinish;
public event TimeLeftChangedEventHandler TimeLeftChanged;
public CountDown(DateTime targetTime)
{
this._target = targetTime;
this._timer = new System.Windows.Forms.Timer();
this._timeMissing = new TimeSpan();
}
public DateTime TargetTime
{
get { return this._target; }
}
public TimeSpan timeMissing
{
get { return this._timeMissing; }
}
public void CountDownStart()
{
_timer.Interval = 1000;
_timer.Tick += new EventHandler(timer_tick);
_timer.Start();
}
protected virtual void timer_tick(Object sender, EventArgs e)
{
System.DateTime now = System.DateTime.Now;
_timeMissing = _target.Subtract(now);

if (!(timeMissing.TotalSeconds > 0))
{
_timer.Stop();

if (CountDownFinish != null)
{
CountDownFinish(this, EventArgs.Empty);
}
}
else
{
if (TimeLeftChanged != null)
{
TimeLeftChanged(this, new TimeLeftDateEventArgs(timeMissing));
}
}
}
}
public class TimeLeftDateEventArgs : EventArgs
{
private int _hours;
private int _minutes;
private int _seconds;
public TimeLeftDateEventArgs(TimeSpan timespan)
{
_hours = timespan.Hours;
_minutes = timespan.Minutes;
_seconds = timespan.Seconds;
}
public int Hours
{ get { return this._hours; } }
public int Minutes
{ get { return this._minutes; } }
public int Seconds
{ get { return this._seconds; } }
}

}

表单类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EventsTry
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CountDown CountDown = new CountDown(new DateTime(2016,06,01,11,35,00));
CountDown.CountDownFinish += new countDownFinishEventHandler(onCountDown);
CountDown.TimeLeftChanged += new TimeLeftChangedEventHandler(onTimeLeft);
CountDown.CountDownStart();
}
private void onCountDown(Object sender, EventArgs e)
{
MessageBox.Show("time expired! ");
}
private void onTimeLeft(Object sender, TimeLeftDateEventArgs e)
{
label1.Text = e.Hours + ":" + e.Minutes + ":" + e.Seconds;
}
}
}

我知道代表countDownFinishEventHandler等于EventHandler。我并没有因为懒惰而改变。

我想对TimeLeftDateEventArgs使用一个内部类,但公共委托对类CountDown是外部的,那么TimeLeftDateEventArgs不能被委托访问。

2-创建TimeLeft属性,该属性显示完成剩余时间,并在计时器刻度事件中减少剩余时间。

您指的是此解决方案?还是其他方式!?

相关内容

  • 没有找到相关文章

最新更新