Thread.ApartmentState 属性
注意:此属性现在已过时。 非过时替代项是 GetApartmentStateSetApartmentState。
获取或设置此线程的单元状态。
**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<ObsoleteAttribute("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", False)> _
Public Property ApartmentState As ApartmentState
用法
Dim instance As Thread
Dim value As ApartmentState
value = instance.ApartmentState
instance.ApartmentState = value
[ObsoleteAttribute("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)]
public ApartmentState ApartmentState { get; set; }
[ObsoleteAttribute(L"The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)]
public:
property ApartmentState ApartmentState {
ApartmentState get ();
void set (ApartmentState value);
}
/** @property */
public ApartmentState get_ApartmentState ()
/** @property */
public void set_ApartmentState (ApartmentState value)
public function get ApartmentState () : ApartmentState
public function set ApartmentState (value : ApartmentState)
属性值
ApartmentState 值之一。初始值为 Unknown。
异常
异常类型 | 条件 |
---|---|
试图将此属性设置为无效单元状态(即单线程单元 (STA) 或多线程单元 (MTA) 以外的状态)。 |
备注
在 .NET Framework 1.0 版和 1.1 版中,ApartmentState 属性将线程标记为指示将在单线程单元或多线程单元中执行。当线程处于 Unstarted 或 Running 线程状态时,可以设置此属性;但一个线程只能设置此属性一次。如果尚未设置此属性,则它将返回 Unknown。
重要事项: |
---|
在 .NET Framework 2.0 版中,如果在启动新线程之前尚未设置它们的单元状态,则这些新线程将初始化为 ApartmentState.MTA。默认情况下,主应用程序线程初始化为 ApartmentState.MTA。无法再通过在第一行代码上设置 System.Threading.ApartmentState 属性将主应用程序线程设置为 ApartmentState.STA。应改用 STAThreadAttribute。 |
在 .NET Framework 2.0 版中,可以使用 /CLRTHREADATTRIBUTE(设置 CLR 线程属性) 链接器选项指定 C++ 应用程序的 COM 线程模型。
提示
应用于此属性 (property) 的 HostProtectionAttribute 属性 (attribute) 具有以下 Resources 属性 (property) 值:Synchronization | SelfAffectingThreading。HostProtectionAttribute 不影响桌面应用程序(这些应用程序通常通过双击图标、键入命令或在浏览器中输入 URL 来启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性。
示例
下面的代码示例阐释了如何设置线程的单元状态。
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
受以下版本支持:1.0、1.1
在 2.0 中过时(编译器警告)