0
Answer

STA and MTA join

Ask a question
Rahul Jain

Rahul Jain

15y
6.9k
1
HI Friends,

I am facing a problem while joining a STA thread to MTA thread. Actually the requirement was some thing like describe below.
1. I have a exe which is using some dialog boxes.
2. These dialog boxes are using MSFlexGrid. This component is a Com   component.
3.so in order to run it with Vb.Net exe i had to run this dialog box in a STA thread.
4. to achive this i created a new thread and set the Apartment status STA.
5.it was working fine when i run the code. but if click on exe than at the time of join it throws an unhandled exception.if i click on continue button of exception dialog box it runs successfully. how ever i can not ignore this exception.
6. exception contains below text (i know its a bog message but i am stuck here for last couple of days.).


************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at MSFlexGridLib.IMSFlexGrid.get_Text()
   at AxMSFlexGridLib.AxMSFlexGrid.get_Text()
   at System.Windows.Forms.Control.OnHandleDestroyed(EventArgs e)
   at System.Windows.Forms.AxHost.DetachWindow()
   at System.Windows.Forms.AxHost.DetachAndForward(Message& m)
   at System.Windows.Forms.AxHost.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

7. COde snippet is like below.

        Dim paramtThread As New Threading.ParameterizedThreadStart(AddressOf DelgInvoke)
         Dim exeThread As New Threading.Thread(paramtThread)
         exeThread.SetApartmentState(Threading.ApartmentState.STA)
         exeThread.Start(mbrs)// mbrs is a structure variable.
         exeThread.Join()

Please let me know if any one have any idea. i am totaly clueless here. it would be a great help.

Regards,
Rahul