Thread.ApartmentState Vlastnost

Definice

Upozornění

The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.

Upozornění

The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.

Získá nebo nastaví stav bytu tohoto vlákna.

public:
 property System::Threading::ApartmentState ApartmentState { System::Threading::ApartmentState get(); void set(System::Threading::ApartmentState value); };
[System.Obsolete("The ApartmentState property has been deprecated.  Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)]
public System.Threading.ApartmentState ApartmentState { get; set; }
[System.Obsolete("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.")]
public System.Threading.ApartmentState ApartmentState { get; set; }
public System.Threading.ApartmentState ApartmentState { get; set; }
[<System.Obsolete("The ApartmentState property has been deprecated.  Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)>]
member this.ApartmentState : System.Threading.ApartmentState with get, set
[<System.Obsolete("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.")>]
member this.ApartmentState : System.Threading.ApartmentState with get, set
member this.ApartmentState : System.Threading.ApartmentState with get, set
Public Property ApartmentState As ApartmentState

Hodnota vlastnosti

Jedna z ApartmentState hodnot. Počáteční hodnota je Unknown.

Atributy

Výjimky

Je proveden pokus o nastavení této vlastnosti na stav, který není platným stavem bytu (stav jiný než byt s jedním vláknem (STA) nebo byt s více vlákny (MTA)).

Příklady

Následující příklad kódu ukazuje, jak nastavit stav bytu vlákna.

using namespace System;
using namespace System::Threading;
ref class ApartmentTest
{
public:
   static void ThreadMethod()
   {
      Thread::Sleep( 1000 );
   }

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( &ApartmentTest::ThreadMethod ) );
   newThread->SetApartmentState(ApartmentState::MTA);
   
   Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(), newThread->GetApartmentState().ToString() );
   newThread->Start();
   
   // Wait for newThread to start and go to sleep.
   Thread::Sleep( 300 );
   try
   {
      
      // This causes an exception since newThread is sleeping.
      newThread->SetApartmentState(ApartmentState::STA);
   }
   catch ( ThreadStateException^ stateException ) 
   {
      Console::WriteLine( "\n{0} caught:\n"
      "Thread is not in the Unstarted or Running state.", stateException->GetType()->Name );
      Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(), newThread->GetApartmentState().ToString() );
   }

}
using System;
using System.Threading;

class ApartmentTest
{
    static void Main()
    {
        Thread newThread = 
            new Thread(new ThreadStart(ThreadMethod));
        newThread.SetApartmentState(ApartmentState.MTA);

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

        newThread.Start();

        // Wait for newThread to start and go to sleep.
        Thread.Sleep(300);
        try
        {
            // This causes an exception since newThread is sleeping.
            newThread.SetApartmentState(ApartmentState.STA);
        }
        catch(ThreadStateException stateException)
        {
            Console.WriteLine("\n{0} caught:\n" +
                "Thread is not in the Unstarted or Running state.", 
                stateException.GetType().Name);
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.ThreadState, newThread.GetApartmentState());
        }
    }

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    }
}
open System.Threading

let threadMethod () = Thread.Sleep 1000

let newThread = Thread threadMethod
newThread.SetApartmentState ApartmentState.MTA

printfn $"ThreadState: {newThread.ThreadState}, ApartmentState: {newThread.GetApartmentState()}"

newThread.Start()

// Wait for newThread to start and go to sleep.
Thread.Sleep 300

try
    // This causes an exception since newThread is sleeping.
    newThread.SetApartmentState ApartmentState.STA

with :? ThreadStateException as stateException ->
    printfn $"\n{stateException.GetType().Name} caught:\nThread is not in the Unstarted or Running state."
    printfn $"ThreadState: {newThread.ThreadState}, ApartmentState: {newThread.GetApartmentState()}"
Imports System.Threading

Public Class ApartmentTest

    <MTAThread> _
    Shared Sub Main()
    
        Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
        newThread.SetApartmentState(ApartmentState.MTA)

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

        newThread.Start()

        ' Wait for newThread to start and go to sleep.
        Thread.Sleep(300)
        Try
            ' This causes an exception since 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.GetApartmentState())
        End Try

    End Sub

    Shared Sub ThreadMethod()
        Thread.Sleep(1000)
    End Sub

End Class

Poznámky

Vlastnost ApartmentState je zastaralá. Zastaralé alternativy jsou GetApartmentState metoda pro načtení stavu bytu a SetApartmentState metoda pro nastavení stavu bytu.

V rozhraní .NET Framework verze 1.0 a 1.1 ApartmentState označí vlastnost vlákno označující, že se spustí v jednovláknovém nebo vícevláknovém bytě. Tuto vlastnost lze nastavit, pokud je vlákno ve Unstarted stavu nebo Running , ale pro vlákno lze nastavit pouze jednou. Pokud vlastnost není nastavená, vrátí Unknown.

Pokus o použití ApartmentState vlastnosti k nastavení stavu bytu vlákna, jehož stav bytu již byl nastaven, je ignorován. Metoda však SetApartmentState v tomto případě vyvolá výjimku InvalidOperationException .

Důležité

V rozhraní .NET Framework verze 2.0 jsou nová vlákna inicializována, jako ApartmentState.MTA by jejich stav bytu nebyl před spuštěním nastaven. Hlavní vlákno aplikace je ve výchozím nastavení inicializováno na ApartmentState.MTA . Hlavní vlákno aplikace již nelze nastavit na ApartmentState.STA hodnotu nastavením System.Threading.ApartmentState vlastnosti na prvním řádku kódu. Místo toho použijte .STAThreadAttribute

V rozhraní .NET Framework verze 2.0 můžete určit model vláken modelu COM pro aplikaci C++ pomocí možnosti linkeru /CLRTHREADATTRIBUTE (Nastavit atribut vlákna CLR).

Platí pro

Viz také