星期四, 12月 28, 2006

Visual Studio 2003自動加上的[STAThread] Tag

這是描述程式是哪種Thread型態:
  • Single-threaded Apartment[STA]

  • Multi-threaded Apartment[MTA]
稍微測試了一下,Visual Studio 2005在開新的C#專案時,並不會自動在Class宣告上加上這行[STAThread];所以應該是Visual Studio 2003才會這樣做。
至於詳細的差別,就要講到Single-thread與Multi-thread和COM的關係了。
When the STAThreadAttribute is applied, it changes the apartment state of the current thread to be single threaded. Without getting into a huge discussion about COM and threading, this attribute ensures the communication mechanism between the current thread and other threads that may want to talk to it via COM. When you're using Windows Forms, depending on the feature you're using, it may be using COM interop in order to communicate with operating system components. Good examples of this are the Clipboard and the File Dialogs.

Windows Forms is not supported within a MTA or free threaded apartment.Applications using Windows Forms should always declare the apartment style they're using, as some other component could initialize the apartment state of thread improperly.

1 則留言:

匿名 提到...

Wacker, Sie hat der bemerkenswerte Gedanke besucht viagra kaufen viagra kaufen [url=http//t7-isis.org]viagra f?r die frau bestellen[/url]