码迷,mamicode.com
首页 > 其他好文 > 详细

定时器例子

时间:2014-10-17 11:43:52      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   sp   div   art   on   cti   bs   

 int countSecond = 5;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DispatcherTimer disTimer = new DispatcherTimer();
    disTimer.Interval = new TimeSpan(0, 0, 0, 1); //参数分别为:天,小时,分,秒。此方法有重载,可根据实际情况调用。
    disTimer.Tick += new EventHandler(disTimer_Tick); //每一秒执行的方法
    disTimer.Start();
        }
        void disTimer_Tick(object sender, EventArgs e)
        {
            if (countSecond == 0)
            {
                MessageBox.Show("结束");
            }
            else
            {
                //判断TextBox是否处于UI线程上
                if (TextBox.Dispatcher.CheckAccess())
                {              
                    TextBox.Text = countSecond.ToString();
                }
                else
                {
                    TextBox.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                   {
                        TextBox.Text = countSecond.ToString();
                    }));
                }
                countSecond--;
            }
        }

定时器例子

标签:io   os   ar   sp   div   art   on   cti   bs   

原文地址:http://www.cnblogs.com/xiaoyaodijun/p/4030553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!