我在Windows窗体(C#3.0,.net 3.5 SP1,VS2008 SP1,Vista)上有一个计时器,即使在它被停止后似乎也能工作.代码是:
using System; using System.Windows.Forms; namespace TestTimer { public partial class Form1 : Form { public Form1() { InitializeComponent(); StartTimer(); } private DateTime deadline; private void StartTimer() { deadline = DateTime.Now.AddSeconds(4); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { int secondsRemaining = (deadline - DateTime.Now).Seconds; if (secondsRemaining <= 0) { timer1.Stop(); timer1.Enabled = false; MessageBox.Show("too slow..."); } else { label1.Text = "Remaining: " + secondsRemaining.ToString() + (secondsRemaining > 1 ? " seconds" : " second"); } } } }
即使在调用timer1.Stop()之后,我仍然会在屏幕上重新发送MessageBoxes.当我按esc时,它会停止.但是,我预计只会出现一个消息框......我该怎么办?添加timer1.Enabled = false不会更改行为.
谢谢
我可能是错的,但是MessageBox.Show()是一个阻塞操作(等待你解除对话框)?如果是这样,只需将Show()调用移到Stop/Enabled行之后?