Thread.TrySetApartmentState(ApartmentState) Thread.TrySetApartmentState(ApartmentState) Thread.TrySetApartmentState(ApartmentState) Thread.TrySetApartmentState(ApartmentState) Method

Definizione

Imposta lo stato dell'apartment di un thread prima che venga avviato.Sets the apartment state of a thread before it is started.

public:
 bool TrySetApartmentState(System::Threading::ApartmentState state);
public bool TrySetApartmentState (System.Threading.ApartmentState state);
member this.TrySetApartmentState : System.Threading.ApartmentState -> bool
Public Function TrySetApartmentState (state As ApartmentState) As Boolean

Parametri

state
ApartmentState ApartmentState ApartmentState ApartmentState

Nuovo stato dell'apartment.The new apartment state.

Restituisce

true se è impostato lo stato dell'apartment; in caso contrario, false.true if the apartment state is set; otherwise, false.

Eccezioni

state non è uno stato di apartment valido.state is not a valid apartment state.

Il thread è già stato avviato.The thread has already been started.

Esempi

L'esempio di codice seguente illustra il GetApartmentState, SetApartmentState, e TrySetApartmentState metodi.The following code example demonstrates the GetApartmentState, SetApartmentState, and TrySetApartmentState methods. L'esempio di codice crea un thread.The code example creates a thread. Prima dell'avvio, il thread GetApartmentState consente di visualizzare iniziale ApartmentState.Unknown lo stato e SetApartmentState imposta lo stato su ApartmentState.STA.Before the thread is started, GetApartmentState displays the initial ApartmentState.Unknown state and SetApartmentState changes the state to ApartmentState.STA. Il TrySetApartmentState metodo quindi restituisce false quando si prova a modificare lo stato su ApartmentState.MTA perché lo stato di apartment è già impostato.The TrySetApartmentState method then returns false when attempting to change the state to ApartmentState.MTA because the apartment state is already set. Se fosse stata tentata la stessa operazione con SetApartmentState, InvalidOperationException sarebbe stata generata l'eccezione.If the same operation had been attempted with SetApartmentState, InvalidOperationException would have been thrown.

Dopo che quest'ultimo viene avviato, il TrySetApartmentState metodo viene usato di nuovo.After the thread is started, the TrySetApartmentState method is used again. Questa volta viene genera ThreadStateException perché il thread è già stato avviato.This time it throws ThreadStateException because the thread has already been started.

using namespace System;
using namespace System::Threading;

void ThreadProc()
{
    Thread::Sleep(2000);
};

void main()
{
    Thread^ t = gcnew Thread(gcnew ThreadStart(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();
}

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

Commenti

Nuovi thread vengono inizializzate come ApartmentState.MTA se lo stato di apartment non è stato impostato prima dell'avvio.New threads are initialized as ApartmentState.MTA if their apartment state has not been set before they are started. Stato di apartment deve essere impostato prima dell'avvio di un thread.Apartment state must be set before a thread is started.

Nota

Il thread principale dell'applicazione viene inizializzato su ApartmentState.MTA per impostazione predefinita.The main application thread is initialized to ApartmentState.MTA by default. L'unico modo per impostare lo stato dell'apartment del thread principale dell'applicazione per ApartmentState.STA consiste nell'applicare il STAThreadAttribute al metodo del punto di ingresso dell'attributo.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.

Il TrySetApartmentState metodo, insieme al GetApartmentState metodo e il SetApartmentState metodo sostituisce il ApartmentState proprietà.The TrySetApartmentState method, along with the GetApartmentState method and the SetApartmentState method, replaces the ApartmentState property.

Si applica a