我正在学习 C# 3.5,我想知道 [STAThread]
在我们的程序中做了什么?
最佳答案
STAThreadAttribute
本质上是 Windows 消息泵与 COM 组件通信的要求。虽然核心 Windows 窗体不使用 COM,但操作系统的许多组件(例如系统对话框)确实使用了此技术。
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.
This blog post (为什么需要 STAThread?)也很好地解释了该要求。如果您想更深入地了解线程模型如何在 CLR 级别工作,请参阅 this MSDN Magazine article from June 2004 (存档,2009 年 4 月)。
关于c# - [STAThread] 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1361033/