ApartmentState 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定 Thread 的 Apartment 狀態。
public enum class ApartmentState
public enum ApartmentState
[System.Serializable]
public enum ApartmentState
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum ApartmentState
type ApartmentState =
[<System.Serializable>]
type ApartmentState =
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ApartmentState =
Public Enum ApartmentState
- 繼承
- 屬性
欄位
MTA | 1 | Thread 會建立並且輸入多執行緒 Apartment。 |
STA | 0 | Thread 會建立並且輸入單一執行緒 Apartment。 |
Unknown | 2 | ApartmentState 屬性尚未設定。 |
範例
下列程式碼範例示範如何設定執行緒的 Apartment 狀態。
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
備註
Apartment 是共用相同執行緒存取需求之物件之進程內的邏輯容器。 相同 Apartment 中的所有物件都可以從 Apartment 中的任何執行緒接收呼叫。 .NET Framework不會使用 Apartment,而 Managed 物件會負責自行以安全線程的方式使用所有共用資源。
因為 COM 類別使用 Apartment,所以 Common Language Runtime 必須在 COM Interop 狀況中呼叫 COM 物件時建立和初始化 Apartment。 受控執行緒可以建立並輸入只允許一個執行緒的單一線程 Apartment (STA) ,或是包含一或多個執行緒的多執行緒 Apartment (MTA) 。 您可以將執行緒的 屬性設定 ApartmentState 為列舉的 ApartmentState 其中一個值,來控制建立的 Apartment 類型。 因為指定的執行緒只能初始化 COM Apartment 一次,所以您無法在第一次呼叫 Unmanaged 程式碼之後變更 Apartment 類型。
如需詳細資訊,請參閱 Thread 、Managed 和 Unmanaged 執行緒和 進階 COM 互通性。