简体   繁体   English

[STAThread] 是做什么的?

[英]What does [STAThread] do?

I am learning C# 3.5 and I want to know what [STAThread] does in our programs?我正在学习 C# 3.5,我想知道[STAThread]在我们的程序中做了什么?

The STAThreadAttribute is essentially a requirement for the Windows message pump to communicate with COM components. STAThreadAttribute本质上是 Windows 消息泵与 COM 组件通信的要求。 Although core Windows Forms does not use COM, many components of the OS such as system dialogs do use this technology.尽管核心 Windows 窗体不使用 COM,但操作系统的许多组件(例如系统对话框)确实使用了此技术。

MSDN explains the reason in slightly more detail: MSDN稍微详细地解释了原因:

STAThreadAttribute indicates that the COM threading model for the application is single-threaded apartment. STAThreadAttribute 表示应用程序的 COM 线程模型是单线程单元。 This attribute must be present on the entry point of any application that uses Windows Forms;该属性必须存在于任何使用 Windows 窗体的应用程序的入口点; if it is omitted, the Windows components might not work correctly.如果省略,Windows 组件可能无法正常工作。 If the attribute is not present, the application uses the multithreaded apartment model, which is not supported for Windows Forms.如果该属性不存在,则应用程序使用 Windows 窗体不支持的多线程单元模型。

This blog post ( Why is STAThread required? ) also explains the requirement quite well. 这篇博文(为什么需要 STAThread? )也很好地解释了该要求。 If you want a more in-depth view as to how the threading model works at the CLR level, see this MSDN Magazine article from June 2004 (Archived, Apr. 2009).如果您想更深入地了解线程模型如何在 CLR 级别工作,请参阅这篇 MSDN 杂志 2004 年 6 月的文章(存档,2009 年 4 月)。

It tells the compiler that you're in a Single Thread Apartment model.它告诉编译器您处于单线程单元模型中。 This is an evil COM thing, it's usually used for Windows Forms (GUI's) as that uses Win32 for its drawing COM for drag and drop COM components (thanks @AnthonyWJones), which is implemented as STA.这是一个邪恶的 COM 东西,它通常用于 Windows 窗体 (GUI),因为它使用 Win32 为其绘图COM 拖放 COM 组件(感谢@AnthonyWJones),它作为 STA 实现。 If you are using something that's STA model from multiple threads then you get corrupted objects.如果您使用的是来自多个线程的 STA 模型,那么您会得到损坏的对象。

This is why you have to invoke onto the Gui from another thread (if you've done any forms coding).这就是为什么您必须从另一个线程调用 Gui(如果您已完成任何表单编码)。

Basically don't worry about it, just accept that Windows GUI threads must be marked as STA otherwise weird stuff happens.基本上不用担心,只需接受必须将 Windows GUI 线程标记为 STA,否则会发生奇怪的事情。

The STAThreadAttribute marks a thread to use the Single-Threaded COM Apartment if COM is needed.如果需要 COM,STAThreadAttribute 标记一个线程使用单线程 COM 单元。 By default, .NET won't initialize COM at all.默认情况下,.NET 根本不会初始化 COM。 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.只有当需要 COM 时,例如创建 COM 对象或 COM 控件时,或者需要拖放时,COM 才会被初始化。 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.当发生这种情况时,.NET 调用底层的 CoInitializeEx 函数,该函数采用一个标志来指示是将线程加入多线程单元还是单线程单元。

Read more info here (Archived, June 2009) 在此处阅读更多信息(2009 年 6 月存档)

and

Why is STAThread required? 为什么需要 STAThread?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM