MTAThreadAttribute 類別

定義

指示應用程式的 COM 執行緒模型為多執行緒 Apartment (Multithreaded Apartment,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
繼承
MTAThreadAttribute
屬性

備註

將此屬性套用至進入點方法 (和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 Interop 的應用程式。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 Interop 初始化應用程式執行緒。The application thread is only initialized for COM interop if the thread actually makes a call to a COM component. 如果未使用 COM Interop, 則執行緒不會初始化, 而MTAThreadAttribute屬性 (如果存在的話) 則沒有任何作用。If COM interop is not used, then the thread is not initialized, and the MTAThreadAttribute attribute, if it is present, has no effect.

從 .NET Framework 版本2.0 開始, COM Interop 的預設執行緒模型取決於您用來開發應用程式的語言, 如下表所示。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.SetApartmentState啟動Thread.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 應用程式, 它會C#呼叫程式庫, 然後再依賴 COM Interop。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 類別的新執行個體。Initializes a new instance of the MTAThreadAttribute 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 的淺層複本 (Shallow Copy)。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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)

適用於

另請參閱