TrySetApartmentState Method

Thread.TrySetApartmentState Method (ApartmentState)

 

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

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True,
	SelfAffectingThreading := True)>
Public Function TrySetApartmentState (
	state As ApartmentState
) As Boolean

Parameters

state
Type: System.Threading.ApartmentState

The new apartment state.

Return Value

Type: System.Boolean

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

Exception Condition
ArgumentException

state is not a valid apartment state.

ThreadStateException

The thread has already been started.

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.

System_CAPS_noteNote

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 TrySetApartmentState method, along with the GetApartmentState method and the SetApartmentState method, replaces the ApartmentState property.

The following code example demonstrates the GetApartmentState, SetApartmentState, and TrySetApartmentState methods. The code example creates a thread. Before the thread is started, GetApartmentState displays the initial ApartmentState.Unknown state and SetApartmentState changes the state to ApartmentState.STA. The TrySetApartmentState method then returns false when attempting to change the state to ApartmentState.MTA because the apartment state is already set. If the same operation had been attempted with SetApartmentState, InvalidOperationException would have been thrown.

After the thread is started, the TrySetApartmentState method is used again. This time it throws ThreadStateException because the thread has already been started.

Imports System
Imports System.Threading

Module Example

    Sub Main()

        Dim t As New Thread(AddressOf ThreadProc)
        Console.WriteLine("Before setting apartment state: {0}", _
            t.GetApartmentState())

        t.SetApartmentState(ApartmentState.STA)
        Console.WriteLine("After setting apartment state: {0}", _
            t.GetApartmentState())

        Dim result As Boolean = _
            t.TrySetApartmentState(ApartmentState.MTA)
        Console.WriteLine("Try to change state: {0}", result)

        t.Start()

        Thread.Sleep(500)

        Try
            t.TrySetApartmentState(ApartmentState.STA)
        Catch ex As ThreadStateException
            Console.WriteLine("ThreadStateException occurs " & _
                "if apartment state is set after starting thread.")
        End Try

        t.Join()
    End Sub

    Sub ThreadProc()
        Thread.Sleep(2000)
    End Sub
End Module

' This code example produces the following output:
'
'Before setting apartment state: Unknown
'After setting apartment state: STA
'Try to change state: False
'ThreadStateException occurs if apartment state is set after starting thread.

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft