Threading C# Help

COM uses apartment models to relieve the programmer of having to deal with threading issues.

However, this also adds some more complexity.

Different apartment types have been added with
different releases of the operating system.

This section discusses the single-threaded apartment and the
multithreaded apartment.

Single-threaded Apartment

The single-threaded apartment (STA) was introduced with Windows NT 3.51.

With an STA, only one thread (the thread that created the instance) is allowed to access the component. However, it is legal to
have multiple STAs inside one process, the inner rectangles with the lollipop represent COM components.

Components and threads (curved arrows) are surrounded by apartments.

The outer rectangle represents a process.

With STAs, there’s no need to protect instance variables from multiple thread access, because this protection is provided by a COM facility, and only one thread accesses the component.

A COM object that is not programmed with thread safety marks the requirements for an STA in the registry with the registry key ThreadingModel set to Apartment.

Multithreaded Apartment

Windows NT 4.0 introduced the concept of a multithreaded apartment (MTA).

With an MTA, multiple threads can access the component Simultaneously.

with one MTA arid two STAs.




A COM object programmed with thread safety in-mind marks the requirement for an MTA in the registry
with the key ThreadingModel set to Free.

The value Both is used for thread-safe COM objects that
don’t mind the apartment type.

Visual Basic 6.0 didn’t offer support for multithreaded apartments.

If you’re using COM objects that have been developed with VB6 that’s an important issue to know.

Posted on October 27, 2015 in Interoperability

Share the Story

Back to Top
Share This