STAThreadAttribute STAThreadAttribute STAThreadAttribute STAThreadAttribute Class

定义

指示应用程序的 COM 线程模型是单线程单元 (STA)。Indicates that the COM threading model for an application is single-threaded apartment (STA).

public ref class STAThreadAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class STAThreadAttribute : Attribute
type STAThreadAttribute = class
    inherit Attribute
Public NotInheritable Class STAThreadAttribute
Inherits Attribute
继承
STAThreadAttributeSTAThreadAttributeSTAThreadAttributeSTAThreadAttribute
属性

注解

将此特性应用于入口点方法 ( Main() C# 和 Visual Basic 中的方法)。Apply this attribute to the entry point method (the Main() method in C# and Visual Basic). 它不起对其他方法。It has no effect on other methods. 若要设置你的代码中启动的线程的单元状态,请使用Thread.SetApartmentStateThread.TrySetApartmentState方法,然后再启动线程。To set the apartment state of threads you start in your code, use the Thread.SetApartmentState or Thread.TrySetApartmentState method before starting the thread.

备注

有关 COM 线程模型的概述,请参阅理解和使用 COM 线程模型For an overview of COM threading models, see Understanding and Using COM Threading Models.

COM 线程模型仅适用于使用 COM 互操作的应用程序。COM threading models only apply to applications that use COM interop. COM 线程模型可设置为单线程单元或多线程的单元中。The COM threading model can be set to single-threaded apartment or multithreaded apartment. 如果线程实际上可以对 COM 组件的调用,应用程序线程仅初始化为 COM 互操作。The application thread is only initialized for COM interop if the thread actually makes a call to a COM component. 如果未使用 COM 互操作,则该线程未初始化,和STAThreadAttribute属性,如果存在,没有任何影响。If COM interop is not used, then the thread is not initialized, and the STAThreadAttribute attribute, if it is present, has no effect.

线程模型为 COM 互操作的默认从.NET Framework 2.0 版开始,取决于在其中开发应用程序,如下表所示的语言。Starting with the .NET Framework version 2.0, the default threading model for COM interop depends on the language in which you are developing your application, as the following table shows.

语言Language COM 单元模型COM apartment model
C#C# 多线程的单元Multithreaded apartment
C++C++ 多线程的单元Multithreaded apartment
Visual BasicVisual Basic 单线程单元Single-threaded apartment

若要更改这些默认值,请使用STAThreadAttribute属性设置为应用程序或调用的线程模型Thread.SetApartmentStateThread.TrySetApartmentState方法,然后再启动线程设置为在特定线程的线程模型。To change these defaults, you use the STAThreadAttribute attribute to set the threading model for the application, or call the Thread.SetApartmentState or Thread.TrySetApartmentState method before starting the thread to set the threading model for a particular thread. 在 c + +,您还可以使用/CLRTHREADATTRIBUTE链接器选项以指定的单元模型。In C++, you can also use the /CLRTHREADATTRIBUTE linker option to specify the apartment model.

ASP.NET 应用程序应设置ASPCompat的属性@ Page指令true来强制页面 STA 线程池提供服务。ASP.NET applications should set the ASPCompat attribute of the @ Page directive to true to force the page to be serviced by the STA thread pool.

下面是一些在其中你将想要使用的事例STAThreadAttribute属性显式设置为单线程单元线程模型:Here are some of the cases in which you'll want to use the STAThreadAttribute attribute to explicitly set the threading model to single-threaded apartment:

  • 在开发 Windows 窗体应用。You're developing a Windows Forms app. Windows 窗体应用程序必须是单线程或如果它们与通信 Windows 系统组件,如剪贴板或 Windows 公共对话框,如果他们使用系统功能,如拖放功能。Windows Forms apps must be single-threaded if they communicate with Windows system components such as the Clipboard or Windows common dialog boxes, or if they use system features such as drag-and-drop functionality. 适用于 C# Windows 窗体应用程序模板会自动添加STAThreadAttributeC# 项目的属性。The Windows Forms Application template for C# automatically adds the STAThreadAttribute attribute to C# projects. 由于单线程单元模型是适用于 Visual Basic 默认值,没有必要的属性。Because the single-threaded apartment model is the default for Visual Basic, there is no need for the attribute.

  • 要开发一个 C# 应用,调用 Visual Basic 库,它又依赖于 COM 互操作。You're developing a C# app that calls a Visual Basic library, which, in turn, relies on COM interop. 由于单线程单元模型是适用于 Visual Basic 默认值,您应更改应用程序的线程模型到单线程使用STAThreadAttribute属性。Because the single-threaded apartment model is the default for Visual Basic, you should change your app's threading model to single-threaded by using the STAThreadAttribute attribute.

  • 你的应用程序可以对使用单线程单元模型的 COM 组件的调用。Your application makes calls to COM components that use the single-threaded apartment model.

构造函数

STAThreadAttribute() STAThreadAttribute() STAThreadAttribute() STAThreadAttribute()

初始化 STAThreadAttribute 类的新实例。Initializes a new instance of the STAThreadAttribute class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

属性

TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

适用于

另请参阅