Apartments: The Classic COM Solution

The solution used by classic COM is deceptively simple: each component declares its synchronization needs. Classic COM makes sure that instances (objects) of that class always reside in an execution context that fits their declared requirements, hence the term apartment. A component declares its synchronization needs by assigning a value to its ThreadingModel named-value in the Registry. The value of ThreadingModel determines the component’s threading model. The available values under classic COM are Apartment, Free, Both or no value at all.

Components that set their threading model to Apartment or leave it blank indicate to COM that they cannot handle concurrent access. COM places these objects in a single-threaded environment called a single-threaded apartment (STA). STA objects always execute on the same STA thread, and therefore do not have to worry about concurrent access from multiple threads.

Components that are capable of handling concurrent access from multiple clients on multiple threads set their threading model to Free. COM places such objects in a multithreaded apartment (MTA).

Components that would like to always be in the same apartment as their client set their threading model to Both. Note that a Both component must be capable of handling concurrent access from multiple clients on multiple threads because its client may be in the MTA.

As discussed in Chapter 2, classic COM marshals away the thread differences between the ...

Get COM & .NET Component Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.