Thread.ApartmentState Właściwość

Definicja

Przestroga

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

Przestroga

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

Przestroga

Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.

Pobiera lub ustawia stan chłoniaka tego wątku.

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("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
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
[<System.Obsolete("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")>]
member this.ApartmentState : System.Threading.ApartmentState with get, set
Public Property ApartmentState As ApartmentState

Wartość właściwości

ApartmentState

Jedna z ApartmentState wartości. Wartość początkowa to Unknown .

Atrybuty

Wyjątki

Podejmowana jest próba ustawienia tej właściwości na stan, który nie jest prawidłowym stanem punktowym (stan inny niż jednowątowy () lub wielowątkowy STA MTA ()).

Przykłady

W poniższym przykładzie kodu pokazano, jak ustawić stan cypki wątku.

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);
    }
}
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

Uwagi

Właściwość ApartmentState jest przestarzała. Nie przestarzałe alternatywy to metoda pobierania stanu GetApartmentState chłoniaka i metoda SetApartmentState ustawiania stanu chłoniaka.

W .NET Framework wersjach 1.0 i 1.1 właściwość oznacza wątek, aby wskazać, że zostanie on wykonany w jednowątkowym lub wielowątkowym ApartmentState obiekcie. Tę właściwość można ustawić, gdy wątek jest w stanie wątku lub , jednak można ją ustawić tylko raz Unstarted Running dla wątku. Jeśli właściwość nie została ustawiona, zwraca wartość Unknown .

Próba użycia właściwości w celu ustawienia stanu chłoniaka wątku, którego stan pępka została już ustawiona, ApartmentState jest ignorowana. Jednak w SetApartmentState tym przypadku metoda zgłasza InvalidOperationException wyjątek .

Ważne

W .NET Framework wersji 2.0 nowe wątki są inicjowane tak, jakby ich stan chłoniaka nie został ustawiony ApartmentState.MTA przed rozpoczęciem. Główny wątek aplikacji jest domyślnie ApartmentState.MTA zainicjowany w programie . Nie można już ustawić głównego wątku aplikacji na , ustawiając właściwość ApartmentState.STA w pierwszym System.Threading.ApartmentState wierszu kodu. Zamiast tego STAThreadAttribute użyj .

W .NET Framework wersji 2.0 można określić model wątkowych modelu COM dla aplikacji języka C++ przy użyciu opcji łączenia /CLRTHREADATTRIBUTE (Ustaw atrybut wątku CLR).

Dotyczy

Zobacz też