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

时间:2016-05-30 17:17:50

标签: c# winforms events timer event-handling

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

类非常简单:接收时间目标,并使用计时器启动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);

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

现在我是另一个问题:

我想在标签上看到留给目标的时间。并将其刷新到任何timer_tick。 而我想保持程序图形的计时器逻辑分离.. 我该怎么做?

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

1 个答案:

答案 0 :(得分:1)

@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属性,显示剩余的完成时间并在计时器tick事件中减少它。

你意味着这个解决方案吗?或其他方式!?