473,503 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Joining a STA thread to a MTA thread

8 New Member
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(Eve ntArgs e)
at System.Windows.Forms.AxHost.DetachWindow()
at System.Windows.Forms.AxHost.DetachAndForward(Messa ge& m)
at System.Windows.Forms.AxHost.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

7. COde snippet is like below.
Expand|Select|Wrap|Line Numbers
  1. ]
  2.         Dim paramtThread As New Threading.ParameterizedThreadStart(AddressOf DelgInvoke)
  3.          Dim exeThread As New Threading.Thread(paramtThread)
  4.          exeThread.SetApartmentState(Threading.ApartmentState.STA)
  5.          exeThread.Start(mbrs)// mbrs is a structure variable.
  6.          exeThread.Join()
  7.  
Expand|Select|Wrap|Line Numbers
  1.  Private Function DelgInvoke(ByVal mbrs2 As Members) As Boolean
  2.       m_bResFromExeThread = False
  3.       m_bResFromExeThread = mbrs2.oJobTask.Execute(mbrs2.oPrinterStatus,   mbrs2.nError, mbrs2.strError)
  4.       Return m_bResFromExeThread
  5.  
  6.    End Function
Please let me know if any one have any idea. i am totaly clueless here. it would be a great help.
Jan 19 '09 #1
13 5418
Plater
7,872 Recognized Expert Expert
Can you make your whole project STA?
Or rather make a secondary thread that calls and starts that thread as a third thread?

Also, why are you using msflexgrid? Are you aware of DataGridView?
Jan 19 '09 #2
mitrrahul
8 New Member
Actually its a very large Vb6.0 to VB.Net migrated project. it contains some 16 components. i can not make whole project as STA.
i know about DataGridView but right now its not possible to change the component, as its being used in many other applications. I had a problem with msflexgrid. so i created a thread and set the apartment as STA. initially it was working fine(or may be i ignored the error). but now it gives me the above mentioned error. i just wanted to know if there is any guideline to join a STA with MTA. i have tried event mechanism also, but it seems like when this thread dies it gives this error.
Jan 19 '09 #3
Plater
7,872 Recognized Expert Expert
Why do you call a Join() right after the Start()?

EDIT: Nevermind that I suppose.

What if you made your own version of Join()?
Something like looping while that thread is still active.
And in your loop you would call
Application.DoEvents
and
Thread.Sleep(100)

And maybe checking a DateTime object if you don't want it to wait forever
Jan 19 '09 #4
mitrrahul
8 New Member
Actually as soon as i say thread.start(). it start executing the function delgInvoke(). Main thread keep waiting for child thread until it finishes. so i am using join() here(for wait forever).
Jan 19 '09 #5
Plater
7,872 Recognized Expert Expert
Right.
But you said Join() is where the exception is thrown right?
So what if you don't use it and implememnt you own blocking/waiting?
Jan 19 '09 #6
mitrrahul
8 New Member
If i don't use Join() and use some other methods like events, still i get the same problem :(.
Jan 19 '09 #7
Plater
7,872 Recognized Expert Expert
All of the creation and destruction of the COM object is happening inside that thread right?
You don't like wait for the thread to close then try to dispose of the com object do you?
Jan 19 '09 #8
mitrrahul
8 New Member
Yes all creation and destruction of com object is happening inside the thread. i have a doubt regarding the destruction of a com object. as i have mentioned i am using MSFlexgrid. and its being used in that thread so can you point out the place where should i dispose that object. i mean as i say thread.start() and just after this call am using join()( orsome other wait mechanism). so should i wait for some time here?
Jan 20 '09 #9
vekipeki
229 Recognized Expert New Member
When you added the MSFlexGrid control to your second form, .Net created the wrapper class (AxMSFlexGrid), which handles the COM interop. You shouldn't worry about disposing it manually, since it will be disposed when your second form is disposed (providing that you simply added it to the form using the VS designer).

You said that you get the same error even without Joining, so the problem shouldn't be with joining. Also, there is no such thing as STA and MTA threads. Using the STA attribute will only tell .Net to initialize COM in STA and marshal all calls to that COM object through a single (the one and only) apartment thread; 'exeThread' is therefore just a thread like any other, marshaling only occurs during calls to COM.

How does the 'mbrs2.oJobTask.Execute' line create the new Form? Are you using ShowDialog or Show with some event handling? I am asking this because in the second case (non modal) you will probably have to use Application.Run to create the form properly.
Jan 20 '09 #10
mitrrahul
8 New Member
Actually mbrs2.oJobTask.Execute() calls a dialog box say dlgUpload. this dialog box is using a Activex control ctrlPrompt and this ctrlPrompt is using another actiavex control UserDefinput. This UserDefInput is using MSFlexgrid.
Actually as you can see in error descirpiton. i am getting the error in MSFlexGrid. so i was confused if i am doing the right things here. i mean should i do some thing explicit ot dispose this msflexgrid?
Jan 20 '09 #11
vekipeki
229 Recognized Expert New Member
But all these controls are from VB6, and you added them using the Designer, not manually, right? I guess you could go through that old code to see if something is not disposed properly related to msflexgrid, but to dispose a child control in another control is not a very good practice.

The only thing that you should do about disposing is to put your dialog form code in a "using" block to ensure that the Dispose method is called immediately after its use.
Jan 20 '09 #12
mitrrahul
8 New Member
All are Vb6.0 to VB.NET 2008 migrated components. when i run the code((Debug/Release mode)) it works fine but if i click on any exe(Debug/Release) it gives me the above mentioned exception.how ever if click on continue button of exception it works ok. but i need to find out the cause because if i enable the JIT debugging then i dont get the excptio dialog box and my threads get hanged some where in middle of the application.
Jan 20 '09 #13
mitrrahul
8 New Member
yes i added them through designer not manually.
Jan 20 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

9
1862
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but...
4
1622
by: sir_alex | last post by:
Hello everybody! I have a couple of questions about threads: the first is, is there the possibility to cancel a thread while it is executing (like the C function thread_cancel), for implementing...
4
4608
by: jason.teen | last post by:
Hi, when i am joining on a Column of Text Type with one of Memo type the resulting entry has funny chinese characters! Has anyone else encountered this before? Is there a cure?? Cheers.
4
1342
by: Mathias Hasselmann | last post by:
Hello, Hope this is the right group for asking this. Didn't see a threading specific newsgroup here. I currently expire unexpected dead-locks within some trivial C# code. Now I wonder if it...
0
1212
by: Lloyd Zusman | last post by:
I have a python-2.5 program running under linux in which I spawn a number of threads. The main thread does nothing while these subsidiary threads are running, and after they all complete, the main...
0
1176
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
2
3329
by: Hans Mull | last post by:
Hi! I'm experimenting with boost::thread. I have a GUI application with a slot function: void someFunction(){...} void Frame::OnOkButtonClick(...) { switch(someInteger) { case 0: break
2
2235
by: Supermansteel | last post by:
I am joining these 2 tables together in Access 2003 and can't figure out the exact way of writing this script......Can anyone help? I have the following SQL: SELECT...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.