2015-06-27 129 views
2

这是我的,为什么我的计时器不停止? 我不知道我在做什么错。 我对C#相当陌生,我试图让它成为我的启动画面隐藏(form1)和我的程序启动(samptool),但是我的程序启动但启动屏幕保持,定时器重置而不是停止。应用程序每6.5秒会在新窗口中打开。C#计时器不停止?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Timers; 
namespace SplashScreen.cs 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 
      timer1.Interval = 250; 
      timer2.Interval = 6500; 
      timer1.Start(); 
      timer2.Start(); 
     } 

     private void pictureBox1_Click(object sender, EventArgs e) 
     { 

     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      this.progressBar1.Increment(5); 
     } 

     private void timer2_Tick(object sender, EventArgs e) 
     { 
      SampTool w = new SampTool(); 
      Form1 m = new Form1(); 
      timer1.Enabled = false; 
      timer1.Stop(); 
      timer2.Enabled = false; 
      timer2.Stop(); 
      m.Hide(); 
      w.Show(); 
     } 
    } 
} 

回答

7

当您使用new关键字,创建一个类的新实例:

Form1 m = new Form1(); 

当您创建一个新的实例,该constructor被调用(构造函数是名为方法与班级一样)。
这将再次运行构造函数中的所有代码,从而创建新的计时器。

要关闭目前的形式,你应该只运行形式Hide方法:

private void timer2_Tick(object sender, EventArgs e) 
{ 
    timer1.Stop(); 
    timer2.Stop(); 
    SampTool sampTool = new SampTool(); 
    sampTool.Show(); 

    Hide(); // call the Forms Hide function. 
}