[英]Timer won't stop after being stopped or disabled
I have a problem with a timer on a form. 我在表单上的计时器有问题。 I've enabled it from the properties and set the interval value 5000. On the tick event, I want to close the current form and open form1, but it's not working.
我已经从属性中启用了它,并将间隔值设置为5000。在tick事件中,我想关闭当前窗体并打开form1,但是它不起作用。 The current form closes and form1 opens every 5 seconds, not only once.
当前窗体关闭,并且form1每5秒打开一次,而不仅仅是打开一次。 What should I do?
我该怎么办? Thank you in advance!
先感谢您!
This is the tick event: 这是滴答事件:
private void timer1_Tick(object sender, EventArgs e)
{
this.Hide();
Form1 frm = new Form1();
frm.ShowDialog();
timer1.Enabled = false;
}
frm.ShowDialog();
is a blocking call, therefore the next line won't get executed until the new form is closed. 是一个阻塞调用,因此在关闭新表单之前,下一行不会执行。 Make sure you start by disabling the timer:
确保通过禁用计时器开始:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
this.Hide();
Form1 frm = new Form1();
frm.ShowDialog();
}
You need to disable the timer before calling the ShowDialog
, so move the timer1.Enabled = false;
您需要在调用
ShowDialog
之前禁用计时器,因此请移动timer1.Enabled = false;
to the first line. 到第一行。 Also I suggest that you add the
frm.Closed
event so that your main form will close after you closed the second form: This is what you want: 另外,我建议您添加
frm.Closed
事件,以便在关闭第二个窗体后关闭主窗体:这就是您想要的:
timer1.Enabled = false;
Hide();
Form1 frm = new Form1();
frm.Closed += (s, args) => Close();
frm.ShowDialog();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.