C#:如何在线程中使用.MDIParent? [英] C#: How to use .MDIParent in a thread ?

查看:239
本文介绍了C#:如何在线程中使用.MDIParent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正面临一个有关从自动化界面启动的mdi子窗口的问题.在我的WinForm应用程序中,我有一个大型机,一个CManager自动化类和一个frm子窗口,一旦调用CManager.Create2DWindow,它将显示2d对象.

我正在尝试从vb6创建2d子窗口,在我进入.MDIParent属性之前,此方法一直有效,这会炸毁跨线程异常.为了模拟它,我直接从CManager.Start()
中启动的线程尝试了实验
我有以下代码:

 公共  bool  Start()
{
   线程create2DFromThread =  System.Threading.Thread( System.Threading.ThreadStart( true ;
   create2DFromThread.SetApartmentState(ApartmentState.STA);
   create2DFromThread.Start();
}

私有 无效 Start2DFrom()
{
    frm2D frm = ;
    frm2D =  frm2D();
  
    frm.MdiParent = m_WindowParent; // ->非法的跨线程异常.
    frm.Show();
} 


我也尝试过使用委托,但这不能解决问题.

您是否知道如何解决?

解决方案

问题是您在与m_WindowParent对象不同的线程中创建表单.你不能这样做.

从提供的代码来看,实际上不需要在此线程化任何东西.但是,如果以后要对表单进行处理,那么您需要在新线程中进行处理之前,需要在UI线程中调用frm2D的创建


Hello everyone,

I am in front of an issue about mdi child window launched from an automation interface. In my WinForm application, I have a mainframe, a CManager automation class, and a frm child window which will display a 2d object once the CManager.Create2DWindow is invoked.

I am trying the 2d child window creation from vb6, this works until I come to the .MDIParent property, this blows up the cross-thread exception. To simulate it I have tried the experimentation directly from a thread started in CManager.Start()

I have the following code:

public bool Start()
{
   Thread create2DFromThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.Start2DFrom));
   create2DFromThread.IsBackground = true;
   create2DFromThread.SetApartmentState(ApartmentState.STA);
   create2DFromThread.Start();
}

private void Start2DFrom()
{
    frm2D frm = null;
    frm2D = new frm2D();
  
    frm.MdiParent = m_WindowParent;  // --> Illegal Cross Thread Exception.
    frm.Show();
}


I have also tried with delegate but this does not fix the issue.

Do you have an idea how this could be solved ?

Thank you very much in advance.

解决方案

The problem is your are creating your form in a different thread than the m_WindowParent object. You can''t do this.

From the code provided, there''s really no need to thread anything there. However, if you''re going to do something with the form afterwards, then you can going to need to Invoke the creation of the frm2D in the UI thread before doing processing in your new thread


这篇关于C#:如何在线程中使用.MDIParent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆