MTAThreadAttribute MTAThreadAttribute MTAThreadAttribute MTAThreadAttribute Class

定义

指示应用程序的 COM 线程模型为多线程单元 (MTA)。Indicates that the COM threading model for an application is multithreaded apartment (MTA).

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

注解

将此特性应用于入口点方法 ( 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 互操作,则该线程未初始化,和MTAThreadAttribute属性,如果存在,没有任何影响。If COM interop is not used, then the thread is not initialized, and the MTAThreadAttribute 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

若要更改这些默认值,请使用MTAThreadAttribute属性设置为应用程序或调用的线程模型Thread.SetApartmentStateThread.TrySetApartmentState方法,然后再启动线程设置为在特定线程的线程模型。To change these defaults, you use the MTAThreadAttribute 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 (设置 CLR 线程特性)链接器选项以指定的单元模型。In C++, you can also use the /CLRTHREADATTRIBUTE (Set CLR Thread Attribute) linker option to specify the apartment model.

想要使用的用例的一些MTAThreadAttribute属性来显式设置为多线程单元的线程模型包括以下:Some of the cases in which you want to use the MTAThreadAttribute attribute to explicitly set the threading model to multithreaded apartment include the following:

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

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

构造函数

MTAThreadAttribute() MTAThreadAttribute() MTAThreadAttribute() MTAThreadAttribute()

初始化 MTAThreadAttribute 类的新实例。Initializes a new instance of the MTAThreadAttribute 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)

适用于

另请参阅