首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

窗口创建句柄时异常

2012-09-02 
窗口创建句柄时错误下面是我的代码,我在一个窗体中创建了两个system.timer.timer,但是当关闭窗体时,如果任

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

[解决办法]
关闭窗体前,先将timer设置为false,因为关闭窗体时会销毁对象,你timer再访问的话就报错
[解决办法]
。。你都先dispose了

热点排行