[英]Timer won't stop after being stopped or disabled
我在表單上的計時器有問題。 我已經從屬性中啟用了它,並將間隔值設置為5000。在tick事件中,我想關閉當前窗體並打開form1,但是它不起作用。 當前窗體關閉,並且form1每5秒打開一次,而不僅僅是打開一次。 我該怎么辦? 先感謝您!
這是滴答事件:
private void timer1_Tick(object sender, EventArgs e)
{
this.Hide();
Form1 frm = new Form1();
frm.ShowDialog();
timer1.Enabled = false;
}
frm.ShowDialog();
是一個阻塞調用,因此在關閉新表單之前,下一行不會執行。 確保通過禁用計時器開始:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
this.Hide();
Form1 frm = new Form1();
frm.ShowDialog();
}
您需要在調用ShowDialog
之前禁用計時器,因此請移動timer1.Enabled = false;
到第一行。 另外,我建議您添加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.