我在多个窗口形式中使用此代码时遇到麻烦:
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/