试试这个代码
Form1 form1 = new Form1();
//Subscribe event here
form1.MdiParent = this;
form1.Show();
这对我的作品
我不知道为什么你的代码不能正常工作,我会回来,一旦我得到了答案。
编辑:我现在得到了答案。
ISynchronizationInvoke's
成员(Invoke
和BeginInvoke
)由Control
类实现如下。
- 获取其创建窗口线程的上下文。
- 生成使用
RegisterWindowMessage
- 一个新的窗口MESSAGEID封装我们作为一个参数
ThreadMethodEntry
通过委托其添加到控件的内部Queue
- 帖子消息线程队列使用
PostMessage
- 把手
WndProc
通过RegisterWindowMessage
返回MESSAGEID监听messageId
,然后取消队列ThreadMethodEntry
并调用委托。
这里出了什么问题?
Form1中Form1中=新Form1中(); form1.Show(); form1.MdiParent = this;
Form.Show
某种程度上导致调用OnLoad
方法,那就是OnShown
被称为异步使用BeginInvoke
if (base.IsHandleCreated)
{
base.BeginInvoke(new MethodInvoker(this.CallShownEvent));//reflected code
}
所以贴WindowMessage
接收设置form1.MdiParent = this;
这反过来又迫使控制前Destroy
它的处理和ReCreate
新手柄。
DestroyHandle
方法由得到它使用PeekMessage
函数吞张贴消息,然后枚举所有元素Queue
,并将其作为状态而不调用的委托,但其标记扔ObjectDisposedException
完成。
Form1 form1 = new Form1();
form1.Show();
Action del =() =>
{
Console.WriteLine("This will never be called");//our custom delegates too fails to be invoked
};
var res = form1.BeginInvoke(del);
//after some more code
form1.EndInvoke(res);//throws `ObjectDisposedException` which was marked previously
form1.MdiParent = this;
投掷ObjectDisposedException("Control")
其实是误导是不是?
注意:您可以在form1.MdiParent = this;
之前使用Application.DoEvents();
轻松解决此问题,因为DoEvents
会立即处理所有未决消息。
我发现它,更新了我的回答 –