Condividi tramite


Thread.ApartmentState Proprietà

Definizione

Attenzione

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

Attenzione

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

Attenzione

Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.

Ottiene o imposta lo stato dell'apartment del thread.

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

Valore della proprietà

ApartmentState

Uno dei valori di ApartmentState. Il valore iniziale è Unknown.

Attributi

Eccezioni

Viene effettuato un tentativo di impostare questa proprietà su uno stato che non è uno stato dell'apartment valido (uno stato diverso da apartment a thread singolo (STA) o apartment con multithreading (MTA)).

Esempio

Nell'esempio di codice seguente viene illustrato come impostare lo stato apartment di un thread.

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

Commenti

La ApartmentState proprietà è obsoleta. Le alternative non obsolete sono il metodo per recuperare lo GetApartmentState stato dell'apartment e il metodo per impostare lo stato SetApartmentState dell'apartment.

Nelle .NET Framework 1.0 e 1.1, la proprietà contrassegna un thread per indicare che verrà eseguito in un apartment a thread singolo o ApartmentState multithreading. Questa proprietà può essere impostata quando il thread si trova nello stato del thread o , ma può essere impostata una Unstarted sola volta per un Running thread. Se la proprietà non è stata impostata, restituisce Unknown .

Un tentativo di utilizzare la ApartmentState proprietà per impostare lo stato dell'apartment di un thread il cui stato apartment è già stato impostato viene ignorato. Tuttavia, il SetApartmentState metodo genera InvalidOperationException un'eccezione in questo caso.

Importante

Nella .NET Framework versione 2.0, i nuovi thread vengono inizializzati come se lo stato dell'apartment non fosse stato ApartmentState.MTA impostato prima dell'avvio. Il thread dell'applicazione principale viene inizializzato su ApartmentState.MTA per impostazione predefinita. Non è più possibile impostare il thread principale dell'applicazione su ApartmentState.STA impostando la proprietà nella prima riga di System.Threading.ApartmentState codice. Usare invece STAThreadAttribute .

In .NET Framework versione 2.0 è possibile specificare il modello di threading COM per un'applicazione C++ usando l'opzione del linker /CLRTHREADATTRIBUTE (Imposta attributo thread CLR).

Si applica a

Vedi anche