System.Threading.Thread.TrySetApartmentState Method

Sets the apartment state of a thread before it is started.

Syntax

public bool TrySetApartmentState (ApartmentState state)

Parameters

state
The new apartment state.

Returns

true if the apartment state is set; otherwise, false.

Remarks

New threads are initialized as ApartmentState.MTA if their apartment state has not been set before they are started. Apartment state must be set before a thread is started.

Note:

The main application thread is initialized to ApartmentState.MTA by default. The only way to set the apartment state of the main application thread to ApartmentState.STA is to apply the STAThreadAttribute attribute to the entry point method.

The Thread.TrySetApartmentState(ApartmentState) method, along with the Thread.GetApartmentState method and the Thread.SetApartmentState(ApartmentState) method, replaces the Thread.ApartmentState property.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0