如何确定我的窗体窗口是否仍然有效?

时间:2018-03-25 16:43:35

标签: c# .net winforms invoke

我有一个窗口,它接收来自工作线程的异步事件。有时这些事件在窗口关闭后进入,当我调用Invoke()来处理事件时,我得到一个例外。

如何测试以确保窗口仍然良好。或者导致在结束生命周期的某个地方处理所有事件?

谢谢 - 戴夫

1 个答案:

答案 0 :(得分:0)

You can check IsHandleCreated before calling invoke, to make sure the form is created and is not destroyed:

if (this.IsHandleCreated)
{
    //this.Invoke ...
}

The property returns false if the form handle is still not created or it has destroyed after closing the form. It also prevents the error of calling invoke before showing the form and before the handle is created.

If for any reason you just care about closing the form, you can check IsDisposed property.

相关问题