Thread.TrySetApartmentState(ApartmentState) Method

Definition

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

public bool TrySetApartmentState (System.Threading.ApartmentState state);

Parameters

state
ApartmentState

The new apartment state.

Returns

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

Exceptions

.NET Core and .NET 5+ only: In all cases on macOS and Linux.

state is not a valid apartment state.

The thread was started and has terminated, or the call is not being made from the thread's context while the thread is running.

Examples

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.

using System;
using System.Threading;

class Example
{
    public static void Main()
    {
        Thread t = new Thread(ThreadProc);
        Console.WriteLine("Before setting apartment state: {0}",
            t.GetApartmentState());

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

        bool result = t.TrySetApartmentState(ApartmentState.MTA);
        Console.WriteLine("Try to change state: {0}", result);

        t.Start();

        Thread.Sleep(500);

        try
        {
            t.TrySetApartmentState(ApartmentState.STA);
        }
        catch (ThreadStateException)
        {
            Console.WriteLine("ThreadStateException occurs " +
                "if apartment state is set after starting thread.");
        }

        t.Join();
    }

    public static void ThreadProc()
    {
        Thread.Sleep(2000);
    }
}

/* 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.
 */

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.

Nota

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.

Applies to

Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1