[STAThread]的含義

[STAThread]
STAThread:Single     Thread     Apartment Thread.(單一線程單元線程)
[]是用來表示Attributes;

[STAThread]
是一種線程模型,用在程序的入口方法上(在C#和VB.NET裏是Main()方法),來指定當前線程的ApartmentState 是STA。用在其他方法上不產生影響。在aspx頁面上可以使用AspCompat = "true" 來達到同樣的效果。這個屬性只在  Com  Interop  有用,如果全部是  managed  code  則無用。簡單的說法:[STAThread]指示應用程序的默認線程模型是單線程單元 (STA)。啓動線程模型可設置爲單線程單元或多線程單元。如果未對其進行設置,則該線程不被初始化。也就是說如果你用的.NET Framework,並且沒有使用COM Interop,一般不需要這個Attribute。其它的還有MTA(多線程套間)、Free  Thread(自由線程)。

[STAThread] attribute指示應用程序的 COM 線程模型是單線程單元。
而於此對應的多線程單元則是 [MTAThread] (多線程單元線程)

COM 線程模型只適用於使用 COM interop 的應用程序。如果將此屬性應用到不使用 COM interop 的應用程序,將沒有任何效果。

COM 線程模型可設置爲單線程單元或多線程單元。如果應用程序線程實際調用了 COM 組件,則僅爲 COM interop 初始化該線程。如果沒有使用 COM interop,則不初始化該線程。

以下是找到的一個資料介紹:
Q. When I create a c# project from scratch in VS.NET, the generated code always have a [STAThread] attribute above the main routine. What does the STAThread attribute really do? Can I change it to MTAThread instead? I have searched website and books, no one seems to explain this well.

Asked by anon. Answered by the Wonk on February 17, 2003

A.

The STAThreadAttribute marks a thread to use the Single-Threaded COM Apartment if COM is needed. By default, .NET won't initialize COM at all. It's only when COM is needed, like when a COM object or COM Control is created or when drag 'n' drop is needed, that COM is initialized. When that happens, .NET calls the underlying CoInitializeEx function, which takes a flag indicating whether to join the thread to a multi-threaded or single-threaded apartment.

A multi-threaded apartment (MTA) in COM is more efficient, since any of a number of RPC threads from a pool can be used to handle a request. However, an object on the MTA thread needs to protect itself from multiple threads accessing it at the same time, so that efficiency comes at a cost.

The single-thread apartment (STA) in COM is inherently single-threaded and therefore no additional thread synchronization is needed. The STA is implemented using the thread's Windows message queue, which is how requests to objects on an STA are serialized. Because of how the STA thread is implemented, calls to objects on that thread are serialized with Windows message handling on that thread, making sure that everything, both the COM objects and the underlying windowing objects, e.g. HWNDs, are all synchronized. This is necessary for UI-oriented COM objects, like controls and drag 'n' drop, which must also be synchronized together with the UI.

When COM is needed .NET will call CoInitializeEx, picking the MTA by default because it's more efficient. However, to get the synchronization needed for controls, windows and drag 'n' drop, you need to mark a thread's entry point with the STAThreadAttribute to let .NET know to initialize the UI thread on the STA. All of the VS.NET project templates put that attribute in to make sure you don't forget:
大致意思是:由於很多COM在.NET環境下如果使用多線程的話,會導致引用的COM不能正常運行,而如果不聲明程序爲STAThread的話,.NET就會自動使用多線程來提高效率,這樣就會導致不可預知的後果。




以下引用另一同輩的發言:http://blog.csdn.net/qilang/archive/2006/06/06/775605.aspx
STA不是單線程的意思.英文爲single threaded apartment.是一種套間(或譯爲公寓)線程模式.

sta thread並不表明應用程式的類型,和應用程序不搭界,恰相反,一個應用程序可以有多個線程.每個線程也可以有多個組件或對象.以前win16位系統的組件線程模式才真正是單線程.這是一種被淘汰了的模式.
線程模式用於處理組件在多線程的環境裏並行與並互的方式.比如套間線程(STAThread)模式中接口跨線程傳遞必須被調度(Marshal),不調度直傳肯定會失敗!而MTA或FreeThread模式中的接口可以不經調度直接傳遞.
這種調度在特定的環境中非常影響性能(可有幾百倍之差).如VB裏只支持STAThread模式.FreeThread模式的組件會在裏面表現成和跨進程一樣慢!
線程模式是微軟的COM基礎中的極其重要的概念.一定要吃透!
我對.net真是一竅不通(沒空去弄,對不起微軟去年的獎賞).但我可以肯定,C#中的[STAThread]屬性是應用程序的套間初始化代碼.可以直接理解成SDK裏的
CoInitialize(NULL); 
初始一個STA套間實際上是相當於開了一個消息窗口,所有調用經此窗口過程調度到組件內.
同理[MTAThread](不知有沒有這個屬性,自已去查)
可以理解成
CoInitializeEx(NULL,COINIT_MULTITHREADED )
這經常是一個對初入com大門的人來說,有一定難度但必須過的一道關.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章