更新:我提供完整代码以供参考
我正在尝试使用Dispatcher方法,而不是Forms.Timer()
我已经在该方法的末尾停止了,但它在停止之前一直循环多次。出了什么问题?
顺便说一下,我必须提到,在计时器if语句中使用了MessageBox.Show()。不知道这是否是原因。
private DispatcherTimer mytimer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
}
void mytimer_Tick(object sender, EventArgs e)
{
if (mytimer.Interval == new TimeSpan(0,0,2))
{
mytimer.Stop();
if (textBox1.Text.Length <= 1)
{
MessageBox.Show("0");
mytimer.Stop();
}
else
{
//do code
MessageBox.Show("more than 0");
mytimer.Stop();
}
}
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
mytimer.Interval = new TimeSpan(0, 0, 2);
mytimer.Tick += new EventHandler(mytimer_Tick);
mytimer.Start();
}
timer.Start()
之前订阅Tick
,并在构造函数中执行。 - sll