如何创建多个线程的形式MDIChild多个、线程、形式、MDIChild

由网友(O_o)分享简介:是否有可能有多个线程形式MDIChild?我有一个ActiveX控件MdiChild形式,可以采取很多处理的CPU,并希望做出一个控制不会影响使用下面的示例code另一个控制。但行frmDoc.MdiParent =我抛出跨线程异常。昏暗frmDoc作为MDIChild昏暗newThread作为新主题(子()frmD...

是否有可能有多个线程形式MDIChild?我有一个ActiveX控件MdiChild形式,可以采取很多处理的CPU,并希望做出一个控制不会影响使用下面的示例code另一个控制。但行frmDoc.MdiParent =我抛出跨线程异常。

 昏暗frmDoc作为MDIChild
昏暗newThread作为新主题(
    子()
        frmDoc =新MDIChild
        frmDoc.MdiParent =我'<  - 这条线抛出跨线程异常。
        Application.Run(frmDoc)
    结束小组
)
newThread.IsBackground = TRUE
newThread.SetApartmentState(ApartmentState.STA)
newThread.Start()
 

抛出System.InvalidOperationException了未处理的:

 消息=跨线程操作无效:
  控制FormMdiApp'从一个线程比它创建于其他线程访问。
来源= System.Windows.Forms的
 

解决方案

GUI元素必须初始化,并仅在主事件循环访问。您可以异步或后台线程处理繁重的计算。

线程 多线程 进程 线程生命周期 线程的五种状态 同步 线程间通信 创建线程的俩种方式

Is it possible to have multiple threaded forms as MDIChild? I have an ActiveX control in MdiChild form that can take a lot of processing CPU, and would like to make that one control will not influence another control by using sample code below. But line frmDoc.MdiParent = Me throws cross threading exception.

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()

Throws System.InvalidOperationException was unhandled:

Message=Cross-thread operation not valid: 
  Control 'FormMdiApp' accessed from a thread other than the thread it was created on.
Source=System.Windows.Forms

解决方案

GUI elements must be initialized and accessed in the main event loop only. You can process heavy calculations asynchronously or in background threads.

阅读全文

相关推荐

最新文章