提前致谢
答案 0 :(得分:1)
这是从生产中拉出来的,所以我确信它有效:
...
_Timer = new DispatcherTimer();
_Timer.Interval = TimeSpan.FromMilliseconds(125);
_Timer.Tick += new EventHandler(_Timer_Tick);
_Timer.IsEnabled = true;
_Timer.Start();
...
void _Timer_Tick(object sender, EventArgs e)
{
try {
...Do your thing here
} catch (Exception x){
Debug.WriteLine("Error: "+x);
}
}
答案 1 :(得分:1)
DispatcherTimer
位于System.Windows.Threading
命名空间。
这样的事情应该有效:
public partial class MainWindow : Window
{
int count = 0;
System.Windows.Threading.DispatcherTimer tmr = new System.Windows.Threading.DispatcherTimer();
public MainWindow()
{
InitializeComponent();
tmr.Interval = new TimeSpan(0, 0, 2);
tmr.Tick += new EventHandler(tmr_Tick);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
tmr.Start();
}
void tmr_Tick(object sender, EventArgs e)
{
label1.Content = count += 1;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
tmr.Stop();
}
}