简体   繁体   English

关闭窗体C#后计时器不会停止

[英]Timer doesn't stop after closing form C#

i create timer in my form, but after i close form, timer still working. 我在表单中创建了计时器,但是在关闭表单后,计时器仍然有效。 I tried different ways to stop this timer, but did not have any success. 我尝试了其他方法来停止此计时器,但没有成功。

what is problem? 有什么问题

private Timer timer100;

private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.timer100 = new System.Windows.Forms.Timer(this.components);
       this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed);
    }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        timer100.Stop();

    }
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        timer100.Dispose();
        timer100 = null;
    }

我没有像注释中提到的jhmt那样将InitializeComponent()放入MainForm的构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM