检测是否处置了控制

Rad*_*u D 14 .net c# winforms

在我的应用程序中,我有一个用户控件,使用线程池执行异步操作.线程池方法如下所示:

private void AsyncFunction(object state)
    {
        ... do the calculation
        //refresh the grid data on the UI thread
        this.BeginInvoke(new MethodInvoker(() =>
                                               {
                          ... update the ui 
                                               }));
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果用户关闭对话框...用户控件被释放,我得到异常:

在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke.

你知道一种检测对话框是否处理的方法吗?我不想在关闭时设置对话框设置的控件属性.还有另一种解决方法吗?

谢谢,

拉杜

max*_*max 21

Control.IsDisposed

  • 实际上,它不能解决100%的情况。请参阅下面的http://stackoverflow.com/questions/4460709/detect-if-control-was-disposed/4460737#4460737 (2认同)

Unm*_*kar 7

你可以使用Control.IsDisposed财产.

try
{
    if(!this.IsDisposed) 
    {
        this.BeginInvoke(new MethodInvoker(() =>

                      {
                                // update my control
                      }
          ));
    }
}
catch ( InvalidOperationException )
{
    // Do something meaningful if you need to.
}
Run Code Online (Sandbox Code Playgroud)