ApartmentState 열거형
Thread의 아파트 상태를 지정합니다.
네임스페이스: System.Threading
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration ApartmentState
‘사용 방법
Dim instance As ApartmentState
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum ApartmentState
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum class ApartmentState
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public enum ApartmentState
SerializableAttribute
ComVisibleAttribute(true)
public enum ApartmentState
멤버
멤버 이름 | 설명 | |
---|---|---|
![]() |
MTA | Thread는 다중 스레드 아파트를 만들고 해당 아파트에 들어갑니다. |
![]() |
STA | Thread는 단일 스레드 아파트를 만들고 해당 아파트에 들어갑니다. |
![]() |
Unknown | ApartmentState 속성이 설정되어 있지 않은 경우 |
설명
아파트는 같은 스레드 액세스 요구 사항을 공유하는 개체에 대한 프로세스 내의 논리적 컨테이너입니다. 같은 아파트 내의 모든 개체는 아파트의 모든 스레드로부터 호출을 받을 수 있습니다. .NET Framework에서는 아파트를 사용하지 않으며 관리되는 개체는 스레드로부터 안전한 방식으로 모든 공유 리소스를 사용해야 합니다.
COM 클래스에서는 아파트를 사용하므로 COM Interop 상황에서 COM 개체를 호출할 경우 공용 언어 런타임에서 아파트를 만들고 초기화해야 합니다. 관리되는 스레드는 하나의 스레드만 허용하는 STA(단일 스레드 아파트)나 하나 이상의 스레드가 포함된 MTA(다중 스레드 아파트)를 만들고 해당 아프트에 들어갈 수 있습니다. 스레드의 ApartmentState 속성을 ApartmentState 열거형의 값 중 하나로 설정하여 아파트의 형식을 제어할 수 있습니다. 주어진 스레드에서는 COM 아파트를 한 번만 초기화할 수 있으므로 비관리 코드에 대한 첫 번째 호출 후에는 아파트 형식을 변경할 수 없습니다.
자세한 내용은 Thread, 관리되는 스레딩과 관리되지 않는 스레딩 및 고급 수준의 COM 상호 운용성을 참조하십시오.
예제
다음 코드 예제에서는 스레드의 아파트 상태를 설정하는 방법을 보여 줍니다.
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading
Public Class ApartmentTest
<MTAThread> _
Shared Sub Main()
Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
newThread.SetApartmentState(ApartmentState.MTA)
' The following line is ignored since
' ApartmentState can only be set once.
newThread.SetApartmentState(ApartmentState.STA)
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
using System;
using System.Threading;
class ApartmentTest
{
static void Main()
{
Thread newThread =
new Thread(new ThreadStart(ThreadMethod));
newThread.SetApartmentState(ApartmentState.MTA);
// The following line is ignored since
// ApartmentState can only be set once.
newThread.SetApartmentState(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.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);
}
}
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);
// The following line is ignored since
// ApartmentState can only be set once.
newThread->SetApartmentState(ApartmentState::STA);
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() );
}
}
import System.*;
import System.Threading.*;
import System.Threading.Thread;
class ApartmentTest
{
public static void main(String[] args)
{
Thread newThread = new Thread(new ThreadStart(ThreadMethod));
newThread.set_ApartmentState(ApartmentState.MTA);
// The following line is ignored since
// ApartmentState can only be set once.
newThread.set_ApartmentState(ApartmentState.STA);
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.get_ThreadState(),
newThread.get_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.set_ApartmentState(ApartmentState.STA);
}
catch (ThreadStateException stateException) {
Console.WriteLine("\n{0} caught:\n" +
"Thread is not in the Unstarted or Running state.",
stateException.GetType().get_Name());
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.get_ThreadState(),newThread.get_ApartmentState());
}
} //main
static void ThreadMethod()
{
Thread.Sleep(1000);
} //ThreadMethod
} //ApartmentTest
플랫폼
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
.NET Compact Framework
2.0에서 지원
참고 항목
참조
System.Threading 네임스페이스
Thread