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");
}