Timer更新UI的合理办法

using System;
using System.Windows;
using System.Timers;
using System.Windows.Threading;

namespace TimerTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
    public partial class MainWindow : Window
    {
        private Timer aTimer = null;

        private delegate void TimerDispatcherDelegate();

        public MainWindow()
        {
            InitializeComponent();

            aTimer = new Timer(1000);
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 1000;
            aTimer.Enabled = true;
        }

        private void OnTimedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal,
                new TimerDispatcherDelegate(updateUI));
        }

        private void updateUI()
        {
            timeLabel.Content = DateTime.Now.ToUniversalTime();
        }
    }
}

http://www.cnblogs.com/zhchbin/archive/2012/03/06/2381693.html

posted @ 2018-12-11 15:49  Alpha燕  阅读(678)  评论(0编辑  收藏  举报