Nedko
Nedko

Reputation: 597

How to make COM object's method be run not in main thread

I have a windows service which implements a COM local server.

When running as application the COM object methods are called in a separate (not in the main) thread which is just fine. Things change when running as service - then COM object methods are called in the context of the service thread which is not cool for me.

I see that it has to do with apartments, MTA, STA and so on.. but I can't figure out how to force COM to call my object methods in separate thread and not in the service one.

May be this has something to do with registration of the com object at the service startup?

My environment is windows 7 + delphi but c++ solutions are welcomed.

Update 2011-04-26:
Kudos to @sharptooth and @Chris Dickson that made me search solution in direction of "message loops".

Since it's STA the app relies on a message pump to deliver the COM messages to the thread that registered the coclass. I relocated the registering of the coclasses into separate thread that have message loop and all com calls are now performed in that thread. I had tried that approach before but forgot about the message loop so this was the missing piece of the puzzle. Thanks guys!

Upvotes: 4

Views: 485

Answers (1)

Chris Edmonds - MSFT
Chris Edmonds - MSFT

Reputation: 61

The objects will be invoked on whichever thread you registered them from. If you would like the objects to be invoked in a separate apartment (STA) you must register the class objects from that apartment. This can be accomplished by creating a separate thread and registering from there.

Upvotes: 1

Related Questions