检测控件是否被处理
在我的应用程序中,我有一个用户控件使用线程池进行异步操作。 线程池的方法是这样的:检测控件是否被处理
private void AsyncFunction(object state)
{
... do the calculation
//refresh the grid data on the UI thread
this.BeginInvoke(new MethodInvoker(() =>
{
... update the ui
}));
}
我的问题是,如果用户关闭对话框...用户控制得到处理,我得到异常:
调用或BeginInvoke不能被称为在一个控件上,直到窗口句柄被创建。
你知道一种检测对话框是否被丢弃的方法吗?我不想在关闭时设置对话框的属性。 有没有解决这个问题的另一种方法?
感谢,
拉杜
+1:一切都说:) – LaGrandMere 2010-12-16 12:26:43
如果它解决了您的问题,请将此标记或任何其他答案作为正确答案。 :) – 2010-12-16 13:00:44
其实它并不能解决100%的情况。看到http://*.com/questions/4460709/detect-if-control-was-disposed/4460737#4460737低于 – Nick 2011-03-28 14:52:10
可以使用Control.IsDisposed
属性。
try
{
if(!this.IsDisposed)
{
this.BeginInvoke(new MethodInvoker(() =>
{
// update my control
}
));
}
}
catch (InvalidOperationException)
{
// Do something meaningful if you need to.
}
我不知道我怎么会错过。我明确地搜索了它:) – 2010-12-16 12:31:28
您需要在调用'BeginInvoke'之前检查'IsDisposed',因为如果控制权被处置,它将失败。 – max 2010-12-16 12:33:21
好..我错过了..我编辑了答案..谢谢 – 2010-12-16 12:39:24
+1。非常有用的问题。 – Nick 2011-03-28 14:51:27