下面是我的代码,我在一个窗体中创建了两个system.timer.timer,但是当关闭窗体时,如果任意一个timer.Enabled=true,则会报错,“用户代码未处理Win32Exception:窗口创建句柄时错误”,报错位置是两个timer的Elapsed事件(两个事件都有可能)中的this.textBox3.Text = hour1.ToString()这句话(就是为textbox.text赋值这句话),以下是代码:
private void button9_Click(object sender, EventArgs e)
        {
            
                timer2.Interval = 1;//表示每1ms刷新一次时钟,每次刷新50个毫秒
                timer2.Elapsed += new ElapsedEventHandler(timer2_Elapsed);
                timer2.AutoReset = true;
                timer2.Enabled = true;                timer4.Interval = 1;//表示每1ms刷新一次时钟,正常是次刷新1个毫秒
                timer4.Elapsed += new ElapsedEventHandler(timer4_Elapsed);
                timer4.AutoReset = true;
                timer4.Enabled = true;
          
        }        void timer2_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (second_mile < 1000)
            {
                second_mile += 50;
                this.textBox_Sec.Text = second.ToString();
                this.textBox_Min.Text = minute.ToString();
                this.textBox_Hour.Text = hour.ToString();
                this.textBox_milesecond.Text = second_mile.ToString();
            }
            else
            {
                second_mile = 0;
                if (second < 59)
                {
                    second += 1;
                    this.textBox_Sec.Text = second.ToString();
                    this.textBox_Min.Text = minute.ToString();
                    this.textBox_Hour.Text = hour.ToString();
                    this.textBox_milesecond.Text = second_mile.ToString();
                }
                else
                {
                    second = 0;
                    if (minute < 59)
                    {
                        minute += 1;
                        this.textBox_Sec.Text = second.ToString();
                        this.textBox_Min.Text = minute.ToString();
                        this.textBox_Hour.Text = hour.ToString();
                        this.textBox_milesecond.Text = second_mile.ToString();
                    }
                    else
                    {
                        minute = 0;
                        hour += 1;
                        this.textBox_Sec.Text = second.ToString();
                        this.textBox_Min.Text = minute.ToString();
                        this.textBox_Hour.Text = hour.ToString();
                        this.textBox_milesecond.Text = second_mile.ToString();
                    }
                }
            }
        }
        void timer4_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (second_mile1 < 1000)
            {
                second_mile1 += 1;
                this.textBox3.Text = hour1.ToString();
                this.textBox4.Text = minute1.ToString();
                this.textBox5.Text = second1.ToString();
                this.textBox6.Text = second_mile1.ToString();
            }
            else
            {
                second_mile1 = 0;
                if (second1 < 59)
                {
                    second1 += 1;
                    this.textBox3.Text = hour1.ToString();
                    this.textBox4.Text = minute1.ToString();
                    this.textBox5.Text = second1.ToString();
                    this.textBox6.Text = second_mile1.ToString();
                }
                else
                {
                    second1 = 0;
                    if (minute1 < 59)
                    {
                        minute += 1;
                        this.textBox3.Text = hour1.ToString();
                        this.textBox4.Text = minute1.ToString();
                        this.textBox5.Text = second1.ToString();
                        this.textBox6.Text = second_mile1.ToString();
                    }
                    else
                    {
                        minute1 = 0;
                        hour1 += 1;
                        this.textBox3.Text = hour1.ToString();
                        this.textBox4.Text = minute1.ToString();
                        this.textBox5.Text = second1.ToString();
                        this.textBox6.Text = second_mile1.ToString();
                    }
                }
            }
            
        }

解决方案 »

  1.   

    关闭窗体前,先将timer设置为false,因为关闭窗体时会销毁对象,你timer再访问的话就报错
      

  2.   

    我在 Form1_FormClosed 和 Form1_Closing中都将timer设置为false了,可还是不行代码如下:为什么啊?有没有窗体关闭前的事件啊???
     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                this.Dispose();
                timer2.Enabled = false;
                timer4.Enabled = false;
                
            }
     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                this.Dispose();
                timer2.Enabled = false;
                timer4.Enabled = false;
            }
      

  3.   

    即便是先设置timer的Enabled值,再dispose也没用的,不是这的问题。