From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Vb.Net, threads and COM controls

I am writing an app that uses the NI-DAQ DIO controls in VB.Net. The aquisition process, that use the controls, is run in a separate thread. When I start the thread 1 or 2 times it work fine. If I wait a few minutes after the app has been idle and then start the thread I get an error "Controls created in one thread canot be parented by another thread" or something like that. I tried dynamically adding and removing the controls from the process thread, but its still throws the error after the app is idle.

Does anybody know whats happening?

Curt
0 Kudos
Message 1 of 4
(5,256 Views)
How are you creating and starting the thread? I'm guessing that you're probably doing something like this:

Dim workerThread As Thread = New Thread(AddressOf MyThreadFunc)
workerThread.Start()

If so, try doing something like this:

Dim workerThread As Thread = New Thread(AddressOf MyThreadFunc)
workerThread.ApartmentState = ApartmentState.STA

workerThread.Start()

If you're using the ActiveX controls in another thread, the apartment state needs to be STA. You can only set the ApartmentState property when the thread is in an unstarted or running state and it can only be set once per thread, so that's why you need to set the property before you actually start the thread.

Please give this a try and post what the results we
re. If this doesn't work, please post a code snippet that shows how you're creating the thread and creating/calling the COM objects. Thanks.

- Elton
0 Kudos
Message 2 of 4
(5,256 Views)
Hi Elton,

Thanks for the reply, but it did not work. I've been doing a little reading and it turns out I will probably have to learn how to use 'Delegates' to handle COM objects in threads... or I might just but Measurement Studio.NET. I think that will be less painfull.

Curt
0 Kudos
Message 3 of 4
(5,256 Views)
This may be a case of the activex control being a UI based control. You can not do any UI operations in a secondary thread without using the invoke calls. I read a MSDN article about this when I was working out a fix for a problem with using MSCOMM in multiple threads. Even though I had not placed the actual COMM control, It was still inherited from the COMM control and therefore would not work in multiple threads. .NET uses a seperate UI thread, and no other threads can update/call UI control without using special invoke methods. Do a search on MSDN and you should find the same article.
0 Kudos
Message 4 of 4
(5,256 Views)