COM, MTA ThreadingModel and Co.

B

bbembi_de

Hello,

I have a COM Object. In my COM object I have the start thread and a
poll thread.
I need to communicate with a COM server from both threads. But this
isn't working.

I read that in C++ every thread has to call CoInitializeEx() but there
isn't such a method in C#.

I tried
Thread.CurrentThread.AprartmentState = ApartmentState.MTA
but this isn't working either.

what can I do?
please help.

bye bembi
 
W

Willy Denoyette [MVP]

| Hello,
|
| I have a COM Object. In my COM object I have the start thread and a
| poll thread.
| I need to communicate with a COM server from both threads. But this
| isn't working.
|
| I read that in C++ every thread has to call CoInitializeEx() but there
| isn't such a method in C#.
|
| I tried
| Thread.CurrentThread.AprartmentState = ApartmentState.MTA
| but this isn't working either.
|

What do you mean with "isn't working"?
Anyway, you should set the thread's apartment before you start the thread.
...
t.ApartmentStae = ApartmentStae.MTA;
t.Start();

Willy.
 
J

juergen

What do you mean with "isn't working"?

I have to call a method to initialize the COM server.
But the COM server is only initialized in the main thread.
In the poll thread it isn't initialized.
I can't call the Initialize method in the poll thread because I get an exception then.

bye bembi
 
J

juergen

I solved it.
In the registry is a key named: HKEY_CLASSES_ROOT\CLSID\.......
Every class has a entry there.
I changed the entry ThreadingModel to "Free" for every class in my COM
object and it worked then.
Now I just wonder if I really have to change all the entries. But this
should be simple to find out.

bye bembi
 
W

Willy Denoyette [MVP]

If you changed it from Apartment to Frre, you have to put Apartment back.
All you are doing is fooling COM and yourself, by this you tell to COM that
an object, which is basically not thread safe, IS thread safe. This is plain
wron, don't do this....
Instead, make sure your thread enters an STA by setting the apartmentstate
to STA.

Willy.



|I solved it.
| In the registry is a key named: HKEY_CLASSES_ROOT\CLSID\.......
| Every class has a entry there.
| I changed the entry ThreadingModel to "Free" for every class in my COM
| object and it worked then.
| Now I just wonder if I really have to change all the entries. But this
| should be simple to find out.
|
| bye bembi
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top