Thread.ApartmentState プロパティ
このスレッドのアパートメント状態を取得または設定します。
Public Property ApartmentState As ApartmentState
[C#]
public ApartmentState ApartmentState {get; set;}
[C++]
public: __property ApartmentState get_ApartmentState();public: __property void set_ApartmentState(ApartmentState);
[JScript]
public function get ApartmentState() : ApartmentState;public function set ApartmentState(ApartmentState);
プロパティ値
ApartmentState 値の 1 つ。初期値は Unknown です。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | このプロパティを、無効なアパートメント状態 (つまりシングルスレッド アパートメント (STA) またはマルチスレッド アパートメント (MTA) 以外のアパートメント状態) に設定しようとしました。 |
解説
ApartmentState プロパティは、スレッドがシングルスレッド アパートメントとマルチスレッド アパートメントのどちらで実行されるかを示します。このプロパティは、スレッドが Unstarted スレッド状態または Running スレッド状態であるときに設定できます。ただし、このプロパティは、1 つのスレッドに対して一度しか設定できません。プロパティが設定されていない場合は、 Unknown を返します。
使用例
[Visual Basic, C#, C++] スレッドのアパートメントの状態を設定する方法の例を次に示します。
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading
Public Class ApartmentTest
Shared Sub Main()
Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
newThread.ApartmentState = ApartmentState.MTA
' The following line is ignored since
' ApartmentState can only be set once.
newThread.ApartmentState = ApartmentState.STA
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.ApartmentState = 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.ApartmentState)
End Try
End Sub
Shared Sub ThreadMethod()
Thread.Sleep(1000)
End Sub
End Class
[C#]
using System;
using System.Threading;
class ApartmentTest
{
static void Main()
{
Thread newThread =
new Thread(new ThreadStart(ThreadMethod));
newThread.ApartmentState = ApartmentState.MTA;
// The following line is ignored since
// ApartmentState can only be set once.
newThread.ApartmentState = ApartmentState.STA;
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.ApartmentState = 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.ApartmentState);
}
}
static void ThreadMethod()
{
Thread.Sleep(1000);
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
__gc class ApartmentTest
{
public :
static void ThreadMethod()
{
Thread::Sleep(1000);
}
};
void main()
{
Thread* newThread =
new Thread(new ThreadStart(0, &ApartmentTest::ThreadMethod));
newThread->ApartmentState = ApartmentState::MTA;
// The following line is ignored since
// ApartmentState can only be set once.
newThread->ApartmentState = ApartmentState::STA;
Console::WriteLine(S"ThreadState: {0}, ApartmentState: {1}",
__box(newThread->ThreadState)->ToString(),
__box(newThread->ApartmentState)->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->ApartmentState = ApartmentState::STA;
}
catch(ThreadStateException* stateException)
{
Console::WriteLine(S"\n{0} caught:\n"
S"Thread is not in the Unstarted or Running state.",
stateException->GetType()->Name);
Console::WriteLine(S"ThreadState: {0}, ApartmentState: {1}",
__box(newThread->ThreadState)->ToString(),
__box(newThread->ApartmentState)->ToString());
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
Thread クラス | Thread メンバ | System.Threading 名前空間 | ThreadState