- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在后台线程中调用 saveFileDialog.ShowDialog()
时出现以下异常:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
根据 this :
To fix the problem, insert the statement:
Threading.Thread.CurrentThread.ApartmentState = Threading.ApartmentState.STA;
in Main right before the Application.Run statement.
但 Application.Run 语句位于 Program.cs 中,它似乎是生成的代码,因此任何更改都可能意外丢失。此外,我找不到在项目或主窗体属性中将当前线程设置为 STA 的方法,但也许我找错了地方。在后台线程中调用 saveFileDialog.ShowDialog()
的正确方法是什么?
最佳答案
解决方案非常简单;只需将其添加到 Main 方法之上 [STAThread]
所以你的主要方法应该是这样的
[STAThread]
static void Main(string[] args)
{
....
}
它对我有用。
关于C# WinForms : How to set Main function STAThreadAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6373645/
This msdn article状态: The WaitAll method is not supported on threads that have STAThreadAttribute. 如果
在后台线程中调用 saveFileDialog.ShowDialog() 时出现以下异常: Current thread must be set to single thread apartment
我是一名优秀的程序员,十分优秀!