Working with Threads

COM+ manages threads for you. Every COM component has a ThreadingModel attribute that you can specify when you develop the component. This property determines how the component's objects are assigned to threads for method execution.

Threads can be associated with two possible types of apartments:

• Single Threaded Apartments (STA)

• Multi Threaded Apartments (MTA)

STAs specify that only one thread is being executed. They are implemented as hidden windows that receive messages and dispatch method calls to the objects residing in the apartment. There can be multiple objects in an STA and there can be multiple STAs in a process.

MTAs have a pool of executing threads. Synchronization must be considered in this scenario. Operating system synchronization mechanisms such as semaphores, critical sections, or mutexes can be used. MTAs are restricted to one per process.

All COM objects that reside in a multithreaded apartment can receive method invocations directly from any of the threads that belong to the same apartment. Threads in a multithreaded apartment use a model referred to as free-threading.

The different types of processes can be defined as follows:

• A process that consists of just one STA is referred to as a single-threaded process.

• A process that has two or more STA and no MTA is referred to as an apartment model process.

• A process that has a MTA and no STA is referred to as a free-threaded process.

• A process that has a MTA and one or more STA is referred to as a mixed model process.

In reality, all processes are apartment model processes, and some apartments have a single thread while others have multiple threads. The threading model applies to an apartment, not to a process. It can also apply to a class of objects, but not a component, such as a DLL; instead, it applies to the object classes within the DLL. Different classes in a DLL can have different threading models.

A process can use both the apartment and free-threaded models, as long as it uses only one free-threaded apartment. It can have more than one single threaded apartment. Invocations to objects in the STAs will be automatically synchronized by Win32 and data will have to be marshaled whenever an apartment barrier is crossed. For more information about threading models, see "Processes, Threads, and Apartments" on MSDN.

When the Thread Neutral Apartment model (TNA) is used, the components are automatically marked as Free Threaded or Apartment. When this model is used, components inherit the same thread type as the thread that made the invocation.

When a thread executes a method contained in a COM object, and the method creates a new object, MTS suspends the current thread and creates a new one to handle the new object. In the TNA model, apartments can have multiple threads.

The following code example demonstrates how to set the apartment state of a thread in Visual Basic 2005; the methods SetApartmentState and GetApartmentState are only supported in Visual Basic 2005.

Imports Microsoft.VisualBasic

Imports System

Imports System.Threading

Public Class ApartmentTest

Shared Sub Main()

Dim newThread As Thread = New Thread(AddressOf ThreadMethod) newThread.SetApartmentState(ApartmentState.MTA)

' The following line is ignored because ' ApartmentState can only be set once. newThread.SetApartmentState(ApartmentState.STA)

Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", _ newThread.ThreadState, newThread.GetApartmentState())

newThread.Start()

' Wait for newThread to start and go to sleep.

Thread.Sleep(300)

' This causes an exception because newThread is sleeping. newThread.SetApartmentState(ApartmentState.STA) Catch stateException As ThreadStateException

Console.WriteLine(vbCrLf & "{0} caught:" & vbCrLf & _

"Thread is not In the Unstarted or Running state.", _ stateException.GetType().Name) Console.WriteLine("ThreadState: {0}, ApartmentState: " & _

"{1}", newThread.ThreadState, newThread.GetApartmentStateO)

End Try End Sub

Shared Sub ThreadMethod()

Thread.Sleep(1000) End Sub

End Class

For more information about COM interoperability and thread programming in .NET, see "Interop Marshaling Overview" in the .NET Framework Developer's Guide on MSDN.

0 0

Post a comment