首页 > 代码库 > WPF单线程定时器 简单实例

WPF单线程定时器 简单实例

        //窗体加载完毕
        void MyMessageBox_Loaded(object sender, RoutedEventArgs e)
        {
            //启动定时期倒计时,多线程计时
            //System.Threading.Timer timer;


            //启动单线程计时
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += ShowTimeTick;
            timer.Start();
        }


        //定时修改计时时间
        private void ShowTimeTick(object sender, EventArgs e)
        {
            timeTick.Content = TimeSpan + "秒后,自动关闭...";
            if (TimeSpan == 0)
            {
                this.Close();
            }
            TimeSpan--;
        }