日期:2014-05-18  浏览次数:21157 次

窗口创建句柄时错误
下面是我的代码,我在一个窗体中创建了两个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();<