在Winform中,我在AsyncCallback期间得到异常:“在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke”。
该应用程序在其它计算机上工作正常。我想知道是否有可能是计算机或网络设置导致了异常。回调是一个跨域的回调,但是所有的数据都是在Winform中接收和显示的,然后就会发生异常。
该异常是一个致命的异常,并出现Windows消息"App.. has stopped“。
编辑1:
public void OpenFormCallback(IAsyncResult ar)
{
// 'this' is a Winform
this.BeginInvoke(new DelegateVoid(SetDataSource));
}它正常工作,现在在例外情况下,存在一个非常快的网络连接,这可能是尚未创建句柄的原因。才意识到这一点。
我是否应该尝试:
public void OpenFormCallback(IAsyncResult ar)
{
// 'this' is a Winform
while (!this.IsHandleCreated) { }
this.BeginInvoke(new DelegateVoid(SetDataSource));
}?冒着永无止境的风险?
编辑2:我试过了:
public void OpenFormCallback(IAsyncResult ar)
{
// 'this' is a Winform
if (IsHandleCreated) { CreateHandle() }
this.BeginInvoke(new DelegateVoid(SetDataSource));
}但是没有显示主窗体,它的状态是“没有响应”。
发布于 2011-07-07 15:21:25
在从回调调用invoke/BeginInvoke之前添加检查yourform.IsHandleCreated
发布于 2011-07-07 18:21:59
试试这个:
public void OpenFormCallback(IAsyncResult ar)
{
while (!this.IsHandleCreated) { Application.DoEvents(); }
this.BeginInvoke(new DelegateVoid(SetDataSource));
}https://stackoverflow.com/questions/6607162
复制相似问题