该范例是时间以秒为单位自动刷新:
点击BUTTUON 触发 定时器时间,每隔一秒执行一次
private void button1_Click(object sender, EventArgs e) { // 创建一个DispatcherTimer实例。 DispatcherTimer newTimer = new DispatcherTimer(); // 将DispatcherTimer的Interval设为1秒。 newTimer.Interval = TimeSpan.FromSeconds(1); // 这样一来OnTimerTick方法每秒都会被调用一次。 newTimer.Tick += OnTimerTick; // 开始计时。 newTimer.Start(); }
触发事件后所要执行的内容如下:
void OnTimerTick(Object sender, EventArgs args) { // 将TextBox的Text属性设置为当前时间。 // ToString()将DateTime转换成string。 txtClock.Text = DateTime.Now.ToString(); }