C++/CLI 中的 Super Basic OpenFileDialog 抛出错误

Posted

技术标签:

【中文标题】C++/CLI 中的 Super Basic OpenFileDialog 抛出错误【英文标题】:Super Basic OpenFileDialog in C++/CLI is throwing error 【发布时间】:2012-03-29 02:29:39 【问题描述】:
OpenFileDialog^ dialog = gcnew OpenFileDialog;
dialog->ShowDialog();

这可能有什么问题? 我在带有 .NET Framework 3.5 的 Windows 7 上使用 Visual Studio 2008 Professional 我创建了一个按钮并添加了一个 Click Function,然后我输入了上面的代码,然后抛出了这个错误:

An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll

我将它从葡萄牙语翻译成英语,因为我的 Visual Studio 用葡萄牙语抛出了错误。

附加信息:当前线程应在 STA 中定义 可以完成 OLE 调用之前的模式(单线程单元)。检查是否 Main 函数检查了 STAThreadAttribute。例外将 仅当调试器附加到线程时才会生成(或者可能 过程)。

这是葡萄牙语的原始错误:

附加信息:O thread atual deve ser definido no modo STA (单线程公寓)ante que chamadas OLE possam ser feitas。 Verifique se a função Main tem STAThreadAttribute marcado。一个例外 somente será gerada se um depurador estiver anexado ao processo。

顺便说一下,所有微软默认的东西都设置好了,Windows Forms Application在创建项目时预设的所有库, 我希望我足够清楚 提前致谢

【问题讨论】:

【参考方案1】:

它要求你输入:

[STA线程]

关于你的主要功能。这意味着 clr 应该启动一个单线程单元。 Windows 窗体无法在 MTA(多线程单元)中工作。

进一步阅读:http://blogs.msdn.com/b/jfoscoding/archive/2005/04/07/406341.aspx

【讨论】:

以上是关于C++/CLI 中的 Super Basic OpenFileDialog 抛出错误的主要内容,如果未能解决你的问题,请参考以下文章

修改 std::string::op[] 的结果是不是合法?

纸模六角大王 Super 5.6 CHS 简体中文版 U20080725+[手册]窗口与工具的概要(PDF格式)

python的类和对象中的super函数的问题

c++/cli 中的 gcroot

System::IntPtr 到 C++/CLI 中的 int*

如何从 C# 访问 C++/CLI 中的类?