番茄时钟——计时器

在这里插入图片描述

public class ProcessCount
    {
        private Int32 _TotalSecond;
        public Int32 TotalSecond
        {
            get { return _TotalSecond; }
            set { _TotalSecond = value; }
        }
 /// <summary>
        /// 构造函数
        /// </summary>
        public ProcessCount(Int32 totalSecond)
        {
            this._TotalSecond = totalSecond;
        }
/// <summary>
        /// 减秒
        /// </summary>
        /// <returns></returns>
        public bool ProcessCountDown()
        {
            if (_TotalSecond == 0)
                return false;
            else
            {
                _TotalSecond--;
                return true;
            }
        }
/// <summary>
        /// 获取分钟显示值
        /// </summary>
        /// <returns></returns>
        public string GetMinute()
        {
            return String.Format("{0:D2}", (_TotalSecond % 3600) / 60);
        }
/// <summary>
        /// 获取秒显示值
        /// </summary>
        /// <returns></returns>
        public string GetSecond()
        {
            return String.Format("{0:D2}", _TotalSecond % 60);
        }
    }
//确定番茄开始时的时间值
            if (lblTaskName.Content == "还没有任务呢,快创建一个吧!")
            {
                MinuteArea.Text = "00";
                SecondArea.Text = "00";
            }
            else
            {
                //时间获取
                if (GlobalData.time==null||GlobalData.time=="")
                {
                    MinuteArea.Text = "25";
                }
                else
                {
                    MinuteArea.Text = GlobalData.time.Trim();
                }
                SecondArea.Text = "00";
                //设置定时器
                timer = new DispatcherTimer();
                timer.Interval = new TimeSpan(10000000);   //时间间隔为一秒
                timer.Tick += new EventHandler(timer_Tick);

//转换成秒数
                Int32 minute = Convert.ToInt32(MinuteArea.Text);
                Int32 second = Convert.ToInt32(SecondArea.Text);
                //处理倒计时的类
                processCount = new ProcessCount(minute * 60 + second);
                CountDown += new CountDownHandler(processCount.ProcessCountDown);
                //开启定时器
                timer.Start();

/// <summary>
        /// 开始or中断番茄
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImOpen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //如果番茄时间未开始,跳转添加番茄界面
            //如果在番茄时间内中断,跳转结束番茄界面
            if (MinuteArea.Text == "00" && SecondArea.Text == "00")
            {
                AddTaskWindow add = new AddTaskWindow();
                add.Show();
            }
            else
            {
                EndTaskWindow end = new EndTaskWindow();
                end.Show();
                MinuteArea.Text = "00";
                SecondArea.Text = "00";
            }
            this.Hide();
        }
        /// <summary>
        /// Timer触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            if (OnCountDown())
            {
                MinuteArea.Text = processCount.GetMinute();
                SecondArea.Text = processCount.GetSecond();
                if (MinuteArea.Text == "00" && SecondArea.Text == "01")
                {
                    CompleTaskWindow comple = new CompleTaskWindow();
                    comple.ShowDialog();
                    this.Hide();
                }
            }
            else
            timer.Stop();
        }
        /// <summary>
        /// 处理事件
        /// </summary>
        public event CountDownHandler CountDown;
        public bool OnCountDown()
        {
            if (CountDown != null)
                return CountDown();
                return false;
        }
        /// <summary>
    /// 处理倒计时的委托
    /// </summary>
    /// <returns></returns>
    public delegate bool CountDownHandler();

DispatcherTimer是常规计时器,它在UI线程上触发其Tick事件;
DispatcherTimer和Timer的区别:
DispatcherTimer是运行在UI线程上的,最大的好处是可以在定时事件中修改UI元素,Timer是运行在独立的线程上的,与UI线程无关,如果需要修改UI控件,则必须委托给调度器this.Dispatcher进行;
而当界面显示比较复杂的时候,UI线程的实时刷新任务比较重,此时就不应该用Dispatcher Timer做严格周期性任务,而应改用Timer或其他的定时器;

DispatcherTimer和System.Timers.Timer的两个小栗子
通过这两个例子可以更好理解两者区别;

C#一共有四种定时器;
1、System.Threading.Timer;
2、System.Timers.Timer;
3、System.Windows.Forms.Timer;
4、System.Windows.Threading.DispatcherTimer(WPF timer);
涉及到WPF界面操作的话,用第四种定时器DispatcherTimer;DispatcherTimer是为WPF专门设计的;
来自这里

计时器里面还涉及到了事件和委托;
委托:

class Program
    {
        public delegate void GreetingDelegate(string name);
        private static void EnglishGreeting(string name)
        {
            Console.WriteLine("Morning, " + name);
        }

        private static void ChineseGreeting(string name)
        {
            Console.WriteLine("早上好, " + name);
        }

        private static void GreetPeople(string name, GreetingDelegate MakeGreeting)
        {
            MakeGreeting(name);
        }
        static void Main(string[] args)
        {
            GreetPeople("Jimmy Zhang", EnglishGreeting);
            GreetPeople("张子阳", ChineseGreeting);
            Console.ReadKey();
        }
    }

先运行完这个简单的小栗子;
在这里插入图片描述

委托将方法当作参数来传递;

事件:
还是以这个例子;

public class GreetingManager
    {
        //定义委托
        public delegate void GreetingDelegate(string name);
        public event GreetingDelegate MakeGreet;
        public void GreetPeople(string name)
        {
            MakeGreet(name);
        }
    }
class Program
    {

        private static void EnglishGreeting(string name)
        {
            Console.WriteLine("Morning, " + name);
        }

        private static void ChineseGreeting(string name)
        {
            Console.WriteLine("早上好, " + name);
        }

        static void Main(string[] args)
        {
            GreetingManager gm = new GreetingManager();
            gm.MakeGreet += EnglishGreeting;
            gm.MakeGreet += ChineseGreeting;
            gm.GreetPeople("Jimmy Zhang");
        }
    }

对于事件,我们只能使用+=,和—=来进行注册和取消注册,无法使用=来进行赋值;
对照字段和属性,委托和事件关系类似,它是对委托实例的一层封装,使得客户端不能随意更改或重置内部的委托列表,只能够允许往列表中增加或者移除委托,使得代码具有更好的封装性;声明一个事件类似于声明了一个进行了封装的委托类型的变量;

在番茄中这两个是怎么用的呢?

//处理倒计时的类
            processCount = new ProcessCount(minute * 60 + second);
            CountDown += new CountDownHandler(processCount.ProcessCountDown);

需要调用ProcessCount类中的减秒这个方法,用委托来实现;

 CountDown += new CountDownHandler(processCount.ProcessCountDown);

用到的是事件;
原文

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页