本文介绍了如何创建多个线程的形式MDIChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能有多个线程形式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.

这篇关于如何创建多个线程的形式MDIChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:37