gpt4 book ai didi

c# - C#Winforms System.Threading.Timer

转载 作者:行者123 更新时间:2023-12-03 13:20:02 24 4
gpt4 key购买 nike

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

  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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com