c# - C#Winforms System.Threading.Timer

标签 c# multithreading winforms timer

我在多个窗口形式中使用此代码时遇到麻烦:

  private void Window_Load(object sender, EventArgs e)
  {   
     new System.Threading.Timer((state) => 
     { 
         BeginInvoke((Action)delegate() 
         {
             if (!CurrentTimeDate.IsHandleCreated) return;
             CurrentTimeDate.Text = "    " +    DateTime.Now.ToString("hh:mm:ss") + "  " + DateTime.Now.ToShortDateString(); 
         }); 

     }, null, 0, 1000);
  }

当我初次显示表单时,它运行良好,然后按窗口表单的关闭按钮。我再次单击该按钮以再次显示该表单,然后显示此错误:

InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created



编辑:我认为它已解决,但代码仍然显示错误

最佳答案

我认为这是计时器,在您关闭表单后,它会继续运行。
尝试保存计时器的实例,并在Form_Closing事件中调用timer.Dispose()

关于c# - C#Winforms System.Threading.Timer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22271741/

相关文章:

c# - wpf中静态数据放在哪里?

java - 多线程环境中的对象深度锁定

c++ - 使用线程在以元音或辅音开头的单词之间交替

java - 自定义 TextView 引发 RuntimeException "Error inflating"

c# - 找出值类似于列表中的任何值

javascript - 使用ajax提交表单并提交阻止并发送文件控件c#的选定文件

c# - 访问者模式,删除需要转换

c# - Form.Closing 未列为表单事件?

c# - 将数据源与 CheckBoxList 一起使用

c# - 在光标位置打开一个小 float 窗口