【发布时间】:2012-09-20 04:04:05
【问题描述】:
是否可以有多个线程表单作为 MDIChild?我有一个 MdiChild 形式的 ActiveX 控件,它可能会占用大量处理 CPU,并且希望通过使用下面的示例代码使一个控件不会影响另一个控件。但是行 frmDoc.MdiParent = Me 会抛出跨线程异常。
Dim frmDoc As MDIChild
Dim newThread As New Thread(
Sub()
frmDoc = New MDIChild
frmDoc.MdiParent = Me '<- this line throws cross threading exception.
Application.Run(frmDoc)
End Sub
)
newThread.IsBackground = True
newThread.SetApartmentState(ApartmentState.STA)
newThread.Start()
抛出 System.InvalidOperationException 未处理:
Message=Cross-thread operation not valid:
Control 'FormMdiApp' accessed from a thread other than the thread it was created on.
Source=System.Windows.Forms
【问题讨论】:
-
你能把这个带有ActiveX控件的表单做成非MDI表单吗?
-
只是在想,这可能在 C++ MFC 应用程序中完成吗?
-
我不了解 C++ MFC,但如果您只是将
frmDoc更改为非 MDI 子窗体,它应该可以工作。当然,当您需要从另一个线程访问它时,您必须小心Invoke。 -
我认为我唯一的选择是关注“MTMDI 示例:演示 MFC 用户界面线程”msdn.microsoft.com/en-us/library/s3wsyb55%28VS.80%29.aspx
-
显示更多关于“线程异常”的细节。