System.Timers.Timer()。WPF DispatcherTimer中的AutoReset等效项

时间:2014-08-14 13:48:18

标签: wpf dispatchertimer

如何手动禁用DispatcherTimer自动触发事件和触发事件?

1 个答案:

答案 0 :(得分:4)

只需在Tick处理程序中调用Stop()或设置IsEnabled = false

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += TimerTick;
timer.Start();
...

private void TimerTick(object sender, EventArgs e)
{
    ((DispatcherTimer)sender).Stop();
    ...
}