NOTE: This API is now obsolete.
The non-obsolete alternative is GetApartmentState()()(). The non-obsolete alternative is SetApartmentState(ApartmentState).
Gets or sets the apartment state of this thread.
Namespace:
System.Threading
Assembly:
mscorlib (in mscorlib.dll)
'Usage
Dim instance As Thread
Dim value As ApartmentState
value = instance.ApartmentState
instance.ApartmentState = value
'Declaration
<ObsoleteAttribute("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", _
False)> _
Public Property ApartmentState As ApartmentState
| Exception | Condition |
|---|
| ArgumentException | An attempt is made to set this property to a state that is not a valid apartment state (a state other than single-threaded apartment (STA) or multithreaded apartment (MTA)). |
In the .NET Framework versions 1.0 and 1.1, the ApartmentState property marks a thread to indicate that it will execute in a single-threaded or multithreaded apartment. This property can be set when the thread is in the Unstarted or Running thread state; however, it can be set only once for a thread. If the property has not been set, it returns Unknown.
In the .NET Framework version 2.0, you can specify the COM threading model for a C++ application using the /CLRTHREADATTRIBUTE (Set CLR Thread Attribute) linker option.
The following code example demonstrates how to set the apartment state of a thread.
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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 1.1, 1.0
Obsolete (compiler warning) in 3.5
Obsolete (compiler warning) in 3.5 SP1
Obsolete (compiler warning) in 3.0
Obsolete (compiler warning) in 3.0 SP1
Obsolete (compiler warning) in 3.0 SP2
Obsolete (compiler warning) in 2.0
Obsolete (compiler warning) in 2.0 SP1
Obsolete (compiler warning) in 2.0 SP2
Reference