[英]Visual studio 2015, C# STAThread not working
STAThread 在 VS 2015 中被忽略了吗? 我有我的主要方法:
[STAThread]
static void Main(string[] args)
{...}
我有一个 function 使用 System.Windows.Forms.OpenFileDialog 并给我以下异常:
{“在进行 OLE 调用之前,必须将当前线程设置为单线程单元 (STA) 模式。确保您的 Main function 上标记了 STAThreadAttribute。仅当调试器附加到进程时才会引发此异常。”}
代码:
try
{
System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog
{
InitialDirectory = @"C:\",
Title = "Browse Text Files",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "txt",
Filter = "txt files (*.txt)|*.txt",
FilterIndex = 2,
RestoreDirectory = true,
ReadOnlyChecked = true,
ShowReadOnly = true
};
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
}catch(Exception e)
{
}
这解决了我的问题。
您可以使用以下 2 个类在 MTA 中使用 OpenFileDialog:
https://acharyarajasekhar.wordpress.com/2015/04/10/c-winforms-openfiledialog-with-mtathread/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.