什么是[STAThread]在c#中的意思?

问题描述:

我找到了关于[STAThread]的所有内容。但是没有得到很好的答案..我希望你能帮助我..

提前谢谢..

I have find every about [STAThread]. but does not get the good answer.. i hope you will help me ..
Thanks in advance..

STA = 单线程公寓 [ ^ ]
STA = Single Thread Apartment[^]


[STAThread]是COM的结转,是Microsoft的组件对象模型,于1993年引入,用于构建软件组件的交互和创建;它是ActiveX,OLE和其他软件抽象层的骨干。请参阅:[ ^ ]。



COM提供了单线程单元(STA)和多线程(MTA)编程模型。



如果使用Microsoft Ribbon Control在Win7 / 8中,您正在使用COM组件。如果您在C#.NET中使用Microsoft提供的控件编写WinForms,您可能正在使用通过COM和ActiveX创建的控件,现在可以与.NET进行交互。



.NET CLR使用COM Interop作为托管.NET类型和COM工具世界之间的一种桥梁,对象[ ^ ]。



深入了解[STA​​Thread]在.NET编程中的含义,我建议你研究Hans Passant的这篇优秀论文:[ ^ ] 。



过度简化:WinForms编程需要使用[STAThread] ...当然,这并不能阻止你创建和使用其他线程,只要他们不触摸UI线程。
[STAThread] is a carry-over from COM, Microsoft's "Component Object Model," introduced in 1993 as way to structure interaction and creation of software components; it was/is the "backbone" of ActiveX, OLE, and other layers of software abstraction. See: [^].

COM provided both single-threaded apartment (STA), and multi-threaded (MTA) programming models.

If use the Microsoft Ribbon Control in Win7/8, you are using a COM component. If you use the Microsoft provided Controls in programming WinForms in C# .NET, you are probably using Controls created with COM, and ActiveX, that now inter-operate with .NET.

The .NET CLR uses COM Interop as a kind of "bridge" between the world of managed .NET Types and COM facilities, and object [^].

To get a deeper sense of what [STAThread] means in .NET programming, I suggest you study this excellent essay by Hans Passant: [^].

To over-simplify: WinForms programming requires the use of [STAThread] ... that, of course, does not stop you from creating and using other threads, as long as they don't "touch" the UI thread.