日期:2014-05-17  浏览次数:21071 次

timer_Tick中为何不能执行button1.PerformClick()?
如下代码,想定时执行按钮单击事件:

  private void timer_Tick(object sender, EventArgs e) //数据定时自动入库
  {
  Debug.Print(DateTime.Now.ToString("yyyy年MM月dd日HH时mm分") + " 不执行");
  if (DateTime.Now.Minute == 15)
  {
  Debug.Print(DateTime.Now.ToString("执行15分入库"));
  btnStart.PerformClick();
  }
  }
  private void btnStart_Click(object sender, EventArgs e)
  {
  Debug.Print("btnstart_click");
  }

输出是:
2012年10月15日11时14分 不执行
执行15分入库
2012年10月15日11时16分 不执行
2012年10月15日11时17分 不执行

问题是在第15分只执行了DEBUG.PRINT语句,但是没有执行btnStart_Click,这是为何呢,请高手指教???

------解决方案--------------------
C# code

this.Invoke(new Action(() => { btnStart.PerformClick(); }));