STAThreadAttribute 类

定义

指示应用程序的 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
继承
STAThreadAttribute
属性

注解

将此特性应用于入口点方法 (和Main() Visual Basic 中C#的方法)。Apply this attribute to the entry point method (the Main() method in C# and Visual Basic). 它不会影响其他方法。It has no effect on other methods. 若要设置在代码中启动的线程的单元状态, 请在Thread.SetApartmentState启动Thread.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.

从 .NET Framework 版本2.0 开始, COM 互操作的默认线程模型取决于你在开发应用程序时所用的语言, 如下表所示。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.SetApartmentState之前Thread.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#自动将STAThreadAttribute属性添加到C#项目中的 Windows 窗体应用程序模板。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 类的新实例。Initializes a new instance of the STAThreadAttribute class.

属性

TypeId

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

(继承自 Attribute)

方法

Equals(Object)

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

(继承自 Attribute)
GetHashCode()

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

(继承自 Attribute)
GetType()

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

(继承自 Object)
IsDefaultAttribute()

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

(继承自 Attribute)
Match(Object)

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

(继承自 Attribute)
MemberwiseClone()

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

(继承自 Object)
ToString()

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

(继承自 Object)

显式界面实现

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

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

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

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

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

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

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

(继承自 Attribute)

适用于

另请参阅