C#中计时器的实现

startTime = DateTime.Now;

DispatcherTimer dt = new DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 1);
dt.Tick += new EventHandler(dt_Tick);//调用函数
dt.Start();

void dt_Tick(object sender, EventArgs e)
{
     timeSpan = DateTime.Now.Subtract(startTime);
     //用TimeSpan来格式化时间
     timer1.Content = string.Format("{0:00}:{1:00}:{2:00}", (int)timeSpan.TotalHours,timeSpan.Minutes, timeSpan.Seconds);
     //用DateTime来格式化时间
      timer2.Content = String.Format("{0:HH:mm:ss}", new DateTime(timeSpan.Ticks));
}

你可能感兴趣的:(timer,object,C#)