By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,462 Members | 1,423 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,462 IT Pros & Developers. It's quick & easy.

COM, MTA ThreadingModel and Co.

P: n/a
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

May 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


<bb*******@lycos.de> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
| 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.
May 16 '06 #2

P: n/a
> 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
May 16 '06 #3

P: n/a
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
May 17 '06 #4

P: n/a
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.

"juergen" <bb*******@lycos.de> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
|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
|
|
May 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.