C# [STAThread] 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1361033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does [STAThread] do?
提问by odiseh
I am learning C# 3.5 and I want to know what [STAThread]
does in our programs?
我正在学习 C# 3.5,我想知道[STAThread]
我们的程序中有什么?
采纳答案by Noldorin
The STAThreadAttribute
is essentially a requirement for the Windows message pump to communicate with COM components. Although core Windows Forms does not use COM, many components of the OS such as system dialogs do use this technology.
这STAThreadAttribute
本质上是 Windows 消息泵与 COM 组件通信的要求。尽管核心 Windows 窗体不使用 COM,但操作系统的许多组件(例如系统对话框)确实使用了此技术。
MSDNexplains the reason in slightly more detail:
MSDN稍微详细地解释了原因:
STAThreadAttribute indicates that the COM threading model for the application is single-threaded apartment. This attribute must be present on the entry point of any application that uses Windows Forms; if it is omitted, the Windows components might not work correctly. If the attribute is not present, the application uses the multithreaded apartment model, which is not supported for Windows Forms.
STAThreadAttribute 指示应用程序的 COM 线程模型是单线程单元。该属性必须出现在任何使用 Windows 窗体的应用程序的入口点上;如果省略它,Windows 组件可能无法正常工作。如果该属性不存在,则应用程序将使用 Windows 窗体不支持的多线程单元模型。
This blog post(Why is STAThread required?) also explains the requirement quite well. 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).
这篇博文(为什么需要 STAThread?)也很好地解释了这个要求。如果您想更深入地了解线程模型如何在 CLR 级别工作,请参阅2004 年 6 月的这篇 MSDN 杂志文章(存档,2009 年 4 月)。
回答by Spence
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, which is implemented as STA. If you are using something that's STA model from multiple threads then you get corrupted objects.
它告诉编译器您处于单线程单元模型中。这是一个邪恶的 COM 东西,它通常用于 Windows 窗体(GUI),因为它使用 Win32 进行绘图,它是作为 STA 实现的。如果您使用的是来自多个线程的 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 否则会发生奇怪的事情。
回答by rahul
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.
STAThreadAttribute 将线程标记为在需要 COM 时使用单线程 COM 单元。默认情况下,.NET 根本不会初始化 COM。只有在需要 COM 时,例如创建 COM 对象或 COM 控件或需要拖放时,才会初始化 COM。发生这种情况时,.NET 调用底层 CoInitializeEx 函数,该函数接受一个标志,指示是将线程加入多线程单元还是单线程单元。
Read more info here(Archived, June 2009)
在此处阅读更多信息(存档,2009 年 6 月)
and
和