【发布时间】:2013-01-30 00:46:39
【问题描述】:
我正在获取“当前线程必须设置为单线程单元 (STA) 模式,然后才能进行 OLE 调用。确保您的 Main 函数上标记了 STAThreadAttribute。仅当调试器附加到进程时才会引发此异常“ 错误。下面是代码。
if (externalButton.Checked == true)
{
// int i = 1;
saveFileDialog.Title = "Save the Proofer Report";
saveFileDialog.Filter = "Document Files (*.doc)|*.doc|Document Files (*.docx)|*.docx";
saveFileDialog.FilterIndex = 0;
saveFileDialog.InitialDirectory = "MyDocuments";
saveFileDialog.FileName = "Proofer Report -- " + Path.GetFileName((string)fileName) + ".doc";
//i.tostring()
saveFileDialog.DefaultExt = ".doc";
saveFileDialog.ShowHelp = true;
saveFileDialog.ShowDialog();-----getting the error here
fname = saveFileDialog.FileName;
}
else
{
fname =(string)fileName;
}
if (fname != "")
{
if (worker.CancellationPending == true)
{
// report progress
worker.ReportProgress(25);
return;
}
程序.cs
[STAthread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
【问题讨论】:
-
哪一行引发了这个错误?
-
您是否确保您的 Main 函数上标记了 STAThreadAttribute?
-
您是否要在后台线程上显示对话框?
-
请参阅我回答中的第二点。不要在后台线程上做 UI 的事情。这是可能的,但是……不。您的模态对话框仍然会发送窗口消息,并且您仍然可以在线程上运行非 UI 的东西。
-
我明白很多...我曾经在 Web 应用程序中编码时遵循相同...所以这个错误...我该怎么办?
标签: c# winforms openfiledialog