DispatcherTimer和Timer(计时器)

System.Windows.Threading.DispatcherTimer dTime;
        System.Timers.Timer timer;
        public MainWindow()
        {
            InitializeComponent();
            if (dTime == null)
            {
                dTime = new System.Windows.Threading.DispatcherTimer();
                dTime.Interval = new TimeSpan(0, 0, 1);
                dTime.Tick += DTime_Tick;
                dTime.Start();
            }
            if (timer == null)
            {
                timer = new System.Timers.Timer();
                timer.Interval = 1000;
                timer.Elapsed += Timer_Elapsed;
                timer.Start();
            }
        }
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(()=> {
                this.timeLable.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }));
        }
        private void DTime_Tick(object sender, EventArgs e)
        {
            this.dTimeLable.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

posted @ 2017-08-28 12:46  多1份努力~  阅读(514)  评论(0编辑  收藏  举报