Thread.TrySetApartmentState(ApartmentState) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在线程启动前设置其单元状态。
public:
bool TrySetApartmentState(System::Threading::ApartmentState state);
public bool TrySetApartmentState (System.Threading.ApartmentState state);
member this.TrySetApartmentState : System.Threading.ApartmentState -> bool
Public Function TrySetApartmentState (state As ApartmentState) As Boolean
参数
- state
- ApartmentState
新的单元状态。
返回
如果设置了单元状态,则为 true
;否则为 false
。
例外
仅适用于 .NET Core 和 .NET 5 +:在 macOS 和 Linux 的所有情况下。
state
不是有效的单元状态。
线程曾启动但已终止,或未在线程运行时从线程的上下文中进行调用。
示例
下面的代码示例演示了 GetApartmentState 、 SetApartmentState 和 TrySetApartmentState 方法。 此代码示例创建一个线程。 启动线程之前,会 GetApartmentState 显示初始状态, ApartmentState.Unknown 并将 SetApartmentState 状态更改为 ApartmentState.STA 。 TrySetApartmentState false
如果 ApartmentState.MTA 已设置了单元状态,则在尝试将状态更改为时,该方法将返回。 如果尝试了同一操作 SetApartmentState ,则 InvalidOperationException 会引发。
线程启动后,将 TrySetApartmentState 再次使用该方法。 这是 ThreadStateException 因为线程已启动。
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.
注解
新线程将初始化,就好像它们 ApartmentState.MTA 的单元状态在启动之前尚未设置。 必须在启动线程之前设置单元状态。
备注
默认情况下,主应用程序线程将初始化为 ApartmentState.MTA 。 将主应用程序线程的单元状态设置为的唯一方法 ApartmentState.STA 是将属性应用于 STAThreadAttribute 入口点方法。
TrySetApartmentState方法连同 GetApartmentState 方法和 SetApartmentState 方法替换 ApartmentState 属性。