关闭表单C#后,计时器不会停止

时间:2015-10-19 00:16:00

标签: c# timer

我在我的表单中创建计时器,但在我关闭表单后,计时器仍在工作。 我尝试了不同的方法来停止这个计时器,但没有取得任何成功。

什么是问题?

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;
    }

1 个答案:

答案 0 :(得分:0)

我没有在-(void)viewDidLoad{ GoodguyFall = [ NSTimer scheduledTimerWithTimeInterval: 0.03 target:self selector:@selector(falldown) userInfo:nil repeats:YES]; } -(void)falldown{ Goodguy.center = (CGPointMake(Goodguy.center.x, Goodguy.center.y + 6)); } if (CGRectIntersectsRect(Goodguy.frame, Building1.frame)){ [GoodguyFall invalidate]; } if (CGRectIntersectsRect(Goodguy.frame, Building2.frame)){ [GoodguyFall invalidate]; } } 的构造函数中将InitializeComponent()作为评论中提到的jhmt