定时器在 C# 中持续触发,无法停止 [英] Timer continuously firing in C#, Not able to stop
本文介绍了定时器在 C# 中持续触发,无法停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我在 windows form C3 应用程序中停止我的计时器吗?我使用设计器在表单中添加了计时器,间隔设置为 1000;单击按钮后等待 5 秒后,我想执行一些操作.请检查代码并建议我.现在的问题是我无限获得 MessageBox2 并且永远不会停止计时器.
Could any one help me to stop my timer in windows form C3 application? I added timer in form using designer and interval is set as 1000; I would like to do some actions after 5 seconds of waiting after button click. Please check the code and advise me. Problem now is I get MessageBox2 infinitely and never gets the timer stop.
static int count;
public Form1()
{
InitializeComponent();
timer1.Tick += timer1_Tick;
}
public void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
while(count>5)
{
....dosome actions...
}
}
private void timer1_Tick(object sender, EventArgs e)
{
count1++;
MessageBox.Show("Messagebox2");
if (count1 == 5)
{
//timer1.Enabled = false; timer1.Stop();
((System.Timers.Timer)sender).Enabled = false;
MessageBox.Show("stopping timer");
}
}
推荐答案
我会让 count
无用,只需使用 timer 1 interval
属性并将您的操作放入timer1_Tick
事件.
I would render the count
useless and just use the timer 1 interval
property and put your actions in the timer1_Tick
event.
public void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
MessageBox.Show("stopping timer");
// Your other actions here
}
这篇关于定时器在 C# 中持续触发,无法停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文