表单关闭后计时器不会触发 [英] Timer doesn't fire after Form closed
本文介绍了表单关闭后计时器不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用 VS 设计器声明的带有 System.Windows.Forms.Timer
的 .NET 表单.计时器工作正常.关闭表单后,即使我重新创建 Timer 对象,计时器也不会触发事件.我已经将表单配置为永远不会关闭:
I have a .NET form with a System.Windows.Forms.Timer
declared using the VS designer. The timer works fine. After I close the form, the timer doesn't fire events even if I recreate the Timer object. I've configured the Form to never close using this:
void MainFormFormClosing(object sender, FormClosingEventArgs e)
{
// never close
e.Cancel = true;
// only hide
this.Visible = false;
}
如何让计时器触发事件?我做错了什么?
How do I make the timer fire events? What am I doing wrong?
推荐答案
我刚试过这个.在表单上添加一个WinForms Timer组件、加载时启动计时器和中调试当前时间调试窗口.对我来说很好用...
I just tried this one. Added a WinForms Timer component on the form, start timer on load, and debug current time in debug window. Workes fine for me...
public frmTimer()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Debug.WriteLine(DateTime.Now.ToLongTimeString());
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Visible = false;
}
这篇关于表单关闭后计时器不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文