Thread.GetApartmentState 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
아파트 상태를 나타내는 ApartmentState 값을 반환합니다.
public:
System::Threading::ApartmentState GetApartmentState();
public System.Threading.ApartmentState GetApartmentState ();
member this.GetApartmentState : unit -> System.Threading.ApartmentState
Public Function GetApartmentState () As ApartmentState
반환
관리되는 스레드의 아파트 상태를 나타내는 ApartmentState 값 중 하나입니다. 기본값은 Unknown입니다.
예제
다음 코드 예제에서는 GetApartmentState , 및 메서드를 보여 줍니다 SetApartmentState TrySetApartmentState . 코드 예제는 스레드를 만듭니다. 스레드가 시작 되기 전에는 GetApartmentState 초기 상태를 표시 하 ApartmentState.Unknown 고 SetApartmentState 상태를로 변경 ApartmentState.STA 합니다. TrySetApartmentState false
ApartmentState.MTA 아파트 상태가 이미 설정 되었기 때문에 메서드는 상태를로 변경 하려고 할 때를 반환 합니다. 로 동일한 작업을 시도한 경우 SetApartmentState 이 throw 됩니다 InvalidOperationException .
스레드가 시작 된 후에는 TrySetApartmentState 메서드가 다시 사용 됩니다. ThreadStateException스레드가 이미 시작 되었기 때문에이 시간이 throw 됩니다.
using namespace System;
using namespace System::Threading;
void ThreadProc()
{
Thread::Sleep(2000);
};
void main()
{
Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
Console::WriteLine("Before setting apartment state: {0}",
t->GetApartmentState());
t->SetApartmentState(ApartmentState::STA);
Console::WriteLine("After setting apartment state: {0}",
t->GetApartmentState());
bool result = t->TrySetApartmentState(ApartmentState::MTA);
Console::WriteLine("Try to change state: {0}", result);
t->Start();
Thread::Sleep(500);
try
{
t->TrySetApartmentState(ApartmentState::STA);
}
catch (ThreadStateException^)
{
Console::WriteLine("ThreadStateException occurs " +
"if apartment state is set after starting thread.");
}
t->Join();
}
/* This code example produces the following output:
Before setting apartment state: Unknown
After setting apartment state: STA
Try to change state: False
ThreadStateException occurs if apartment state is set after starting thread.
*/
using System;
using System.Threading;
class Example
{
public static void Main()
{
Thread t = new Thread(ThreadProc);
Console.WriteLine("Before setting apartment state: {0}",
t.GetApartmentState());
t.SetApartmentState(ApartmentState.STA);
Console.WriteLine("After setting apartment state: {0}",
t.GetApartmentState());
bool result = t.TrySetApartmentState(ApartmentState.MTA);
Console.WriteLine("Try to change state: {0}", result);
t.Start();
Thread.Sleep(500);
try
{
t.TrySetApartmentState(ApartmentState.STA);
}
catch (ThreadStateException)
{
Console.WriteLine("ThreadStateException occurs " +
"if apartment state is set after starting thread.");
}
t.Join();
}
public static void ThreadProc()
{
Thread.Sleep(2000);
}
}
/* This code example produces the following output:
Before setting apartment state: Unknown
After setting apartment state: STA
Try to change state: False
ThreadStateException occurs if apartment state is set after starting thread.
*/
Imports System.Threading
Module Example
Sub Main()
Dim t As New Thread(AddressOf ThreadProc)
Console.WriteLine("Before setting apartment state: {0}", _
t.GetApartmentState())
t.SetApartmentState(ApartmentState.STA)
Console.WriteLine("After setting apartment state: {0}", _
t.GetApartmentState())
Dim result As Boolean = _
t.TrySetApartmentState(ApartmentState.MTA)
Console.WriteLine("Try to change state: {0}", result)
t.Start()
Thread.Sleep(500)
Try
t.TrySetApartmentState(ApartmentState.STA)
Catch ex As ThreadStateException
Console.WriteLine("ThreadStateException occurs " & _
"if apartment state is set after starting thread.")
End Try
t.Join()
End Sub
Sub ThreadProc()
Thread.Sleep(2000)
End Sub
End Module
' This code example produces the following output:
'
'Before setting apartment state: Unknown
'After setting apartment state: STA
'Try to change state: False
'ThreadStateException occurs if apartment state is set after starting thread.
설명
이 메서드는 SetApartmentState 메서드 및 메서드와 함께 속성을 TrySetApartmentState 대체 ApartmentState 합니다.