Search code examples
comblockingsta

Blocking method of an STA COM object is a design defect?


Say a COM object is created on an STA thread. So all calls to this object are serialized in this thread. So if a method of the object's is blocking, all threads that use this object are blocked.

So having a blocking method in an STA COM object is a design defect to be avoided?

If the COM object is free threading, it is OK to have a blocking method?

Thanks


Solution

  • Yes, objects on single-threaded apartments are synchronized via messages and all calls to them are serialized in such way that no more than one method can be called on any such object at any moment of time (also no method can be called more than once at any moment of time). This is by design and is done to achieve a certain degree of thread-safety. Having a long running method by itself is not a big problem unless you introduce a deadlock. Yes, the callers will wait until their calls are run in turn.

    Objects in the multi-threaded apartment are not synchronized via messages - all calls to such objects are done directly without synchronization so more than one or more methods can be called at any object at any moment of time in parallel and it's up to the object to ensure thread-safety. Methods of course can block inside, but care should be taken to not introduce a deadlock.