如何在 WPF 中创建计时器? [英] How do I create a timer in WPF?
本文介绍了如何在 WPF 中创建计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 wpf 计时器的新手,我需要一个代码,每隔 5 分钟就会弹出一个消息框..任何人都可以帮助我了解计时器的简单代码.
I am a newbie in timer in wpf and I need a code that every 5mins there is a message box will pop up. .can anyone help me for the simple code of timer.
这就是我到目前为止所尝试的:
That's what I tried so far:
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private void test()
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// code goes here
}
private void button1_Click(object sender, RoutedEventArgs e)
{
test();
}
推荐答案
在 WPF 中,您使用 DispatcherTimer
.
In WPF, you use a DispatcherTimer
.
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,5,0);
dispatcherTimer.Start();
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// code goes here
}
这篇关于如何在 WPF 中创建计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文