Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我收到异常“调用线程必须是 STA,因为许多 UI 组件都需要这个。” 这很愚蠢,因为正在从 STA 线程中调用给出异常的构造函数!
我真的走投无路了。有没有其他人遇到过这个问题?
答案是这有点烦人,但它确实有效。
我非常厌恶地发现,尽管我使用了该STAThread属性,但直接在 Main 函数中的线程状态是 MTA。显然,调用我的入口点的调试线程决定覆盖该属性并使其成为 MTA 线程。解决方案是创建另一个线程并在其中运行我的应用程序。我这样做了,它奏效了。我讨厌这个,但似乎没有其他人有这个问题。
STAThread
这可能是我设置项目的时髦方式。也就是说,要从面向 .Net 3.5 的 MFC 应用程序(无法帮助)中运行,即使它在 Visual Studio 2010 中也是如此。
无论如何,它有效。