Thread.ApartmentState Thread.ApartmentState Thread.ApartmentState Thread.ApartmentState Property

定義

警告

この API は現在使われていません。

このスレッドのアパートメント状態を取得または設定します。Gets or sets the apartment state of this thread.

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

プロパティ値

ApartmentState 値のいずれか。One of the ApartmentState values. 初期値は Unknown です。The initial value is Unknown.

例外

このプロパティを、有効ではないアパートメント状態に設定しようとしました (シングル スレッド アパートメント (STA) またはマルチ スレッド アパートメント (MTA) 以外の状態)。An attempt is made to set this property to a state that is not a valid apartment state (a state other than single-threaded apartment (STA) or multithreaded apartment (MTA)).

次のコード例は、スレッドのアパートメント状態を設定する方法を示しています。The following code example demonstrates how to set the apartment state of a 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.ApartmentState);

        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

注釈

プロパティApartmentStateは互換性のために残されています。The ApartmentState property is obsolete. 旧式ではない代替手段はGetApartmentState 、アパートメント状態を取得するメソッドSetApartmentStateとアパートメント状態を設定するメソッドです。The non-obsolete alternatives are the GetApartmentState method to retrieve the apartment state and the SetApartmentState method to set the apartment state.

.NET Framework バージョン1.0 および1.1 では、プロパティApartmentStateは、スレッドがシングルスレッドまたはマルチスレッドアパートメントで実行されることを示すようにマークします。In the .NET Framework versions 1.0 and 1.1, the ApartmentState property marks a thread to indicate that it will execute in a single-threaded or multithreaded apartment. このプロパティは、スレッドがUnstartedまたはRunningスレッド状態のときに設定できます。ただし、スレッドに対して一度だけ設定できます。This property can be set when the thread is in the Unstarted or Running thread state; however, it can be set only once for a thread. プロパティが設定されていない場合はUnknown、が返されます。If the property has not been set, it returns Unknown.

ApartmentStateプロパティを使用して、アパートメント状態が既に設定されているスレッドのアパートメント状態を設定しようとしましたが、無視されます。An attempt to use the ApartmentState property to set the apartment state of a thread whose apartment state has already been set is ignored. ただし、このSetApartmentState場合、メソッドInvalidOperationExceptionはをスローします。However, the SetApartmentState method throws a InvalidOperationException in this case.

重要

.NET Framework バージョン2.0 では、新しいスレッドは、アパートメントApartmentState.MTA状態が開始前に設定されていないかのように初期化されます。In the .NET Framework version 2.0, new threads are initialized as ApartmentState.MTA if their apartment state has not been set before they are started. メインアプリケーションスレッドは、既定でApartmentState.MTAに初期化されます。The main application thread is initialized to ApartmentState.MTA by default. 最初のコード行でApartmentState.STA System.Threading.ApartmentStateプロパティを設定することによって、メインアプリケーションスレッドをに設定できなくなりました。You can no longer set the main application thread to ApartmentState.STA by setting the System.Threading.ApartmentState property on the first line of code. 代わりにをSTAThreadAttribute使用してください。Use the STAThreadAttribute instead.

.NET Framework バージョン2.0 では、 /CLRTHREADATTRIBUTE (CLR スレッド属性の設定)リンカー C++オプションを使用して、アプリケーションの COM スレッドモデルを指定できます。In the .NET Framework version 2.0, you can specify the COM threading model for a C++ application using the /CLRTHREADATTRIBUTE (Set CLR Thread Attribute) linker option.

適用対象

こちらもご覧ください