STA thread

C#程序的主函数写[STA Thread] 属性是什么目的?(What is the purpose of the [STA Thread] attribute for the Main method of a C# program? )

C#中,[STAThread]代表什么意思?如何用?

> Single Thread Apartment vs MultiThread Apartment?
 
Correct: With the STAThread attribute, you will be interacting with COM processes in a "Single Threading Apartment" model.   Without it, you will be interacting with COM processes in the "Multiple Threading Apartment" model.
 
> so why do I need it....or why would I want it at some point?
 
You may want to interact with a COM process in a MTA model for performance reasons.   You may want to interact with a COM process in a STA model because of a design requirement.   For example, to use the Windows clipboard (System.Windows.Forms.Clipboard) you must be calling from a thread running in a STA.   If the calling thread was started by your application you can set the ApartmentState (System.Threading.ApartmentState) before starting, but if you want to use the clipboard from your application's main thread, you need to use the System.STAThread attribute on your Main method.
 
> why does Main( ) only function as an entry point when it is declared static?
 
The simple answer is that is just the way that Microsoft designed the language.   One way you can look at this though, is there should only be 1 "instance" of your Main method - the main method has nothing to do with any specific instances of the class it is defined in, and should therefore be static.   In my opinion it might have been a good idea to give the Main method a property similar to a static contructor where it is executed once, and only once.   Anyway, because the Main method is static, you can execute your program without having to create any arbitrary objects.


单线程套间,简单来说所有对于单线程套间中对象的访问都是通过消息来传递的,所以同一时间只有一个线程能够访问单线程套间中的对象。

Windows form 等经常要调用老的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. 

If the application cannot control the apartment state of the current thread, it should start up a new thread.  Here's a quick example:

using System.Threading;

Thread t = new Thread(new ThreadStart(StartNewStaThread));

// Make sure to set the apartment state BEFORE starting the thread.
t.ApartmentState = ApartmentState.STA;
t.Start();

private void StartNewStaThread() {
    Application.Run(new Form1());
}

Instead of using t.ApartmentState, use t.SetApartmentState(ApartmentState.STA);   

If you dont want to fuss with setting it manually, you can also put the [STAThread] attribute on top of StartNewStaThread method:

Thread t = new Thread(new ThreadStart(StartNewStaThread));
t.Start();

[STAThread]
private void StartNewStaThread() {
    Application.Run(new Form1());


 

 

posted on 2008-08-24 16:50  oyl  阅读(910)  评论(0编辑  收藏  举报

导航