Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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.
Posted
Updated 8-Dec-11 0:43am
v4
Comments
SuperMiQi 8-Dec-11 8:42am    
Hello MarqW,

Thank you for your reply.

The goal of this thread is to manage requested from com interface and one request I need to provide is the creation of mdi child window. This is why I was simulating the requirement based on the sample code I provided. How would it be possible then to invoke such a request. Do you mean the main winform needs to support automation interface ?

Thank you very much in advance.

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900