【发布时间】:2010-08-27 13:08:07
【问题描述】:
这是我尝试在程序中打开 OpenFileDialog 时收到的错误消息:
"当前线程必须设置为单线程 OLE 之前的线程单元 (STA) 模式 可以拨打电话。确保您的 主函数具有 STAThreadAttribute 标在上面。这个例外只 如果调试器附加到 过程。”
此错误消息的问题在于我的 Main 方法确实附加了 STAThread 属性。我不知道如何处理这个问题。如果它已经存在,我该如何添加。加倍它不是一个好的选择,我尝试擦除它,构建应用程序,添加它并再次构建它,但没有成功。我只是不明白。
private void btnOldFind_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
openFileDialog1.FileName = "";
openFileDialog1.ShowHelp = false;
openFileDialog1.AutoUpgradeEnabled = true;
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Filter = "Microsoft Installer (*.msi)|*.msi|All Files (*.*)|*.* ";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBoxOldInstallation.Text = openFileDialog1.FileName;
}
}
主要方法是:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
并且没有显式地完成线程。老实说,只是一个非常基本的程序。
EDIT2::
这是完整的错误信息,包括调用堆栈
System.Threading.ThreadStateException 未处理 Message="当前线程必须设置为单线程单元 (STA) 模式,然后才能进行 OLE 调用。确保您的 Main 函数上标记了 STAThreadAttribute。仅当调试器附加到进程时才会引发此异常。" 源="系统.Windows.Forms" 堆栈跟踪: 在 System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner) 在 System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window 所有者) 在 System.Windows.Forms.CommonDialog.ShowDialog() 在 c:\tfs\DocuWare .NET\DocuWare NewGen\src\Tools\MSI_Comparison\MSI_Comparison_GUI\Form1.cs:line 70 中的 MSI_Comparison_GUI.Form1.btnOldFind_Click(Object sender, EventArgs e) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 c:\tfs\DocuWare .NET\DocuWare NewGen\src\Tools\MSI_Comparison\MSI_Comparison_GUI\Program.cs: 18 中的 MSI_Comparison_GUI.Program.Main() 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:
【问题讨论】:
-
你能告诉我们代码,你是如何创建对话框的吗?您是否在程序中创建了另一个线程?
-
已添加为您的阅读乐趣:P
-
你确定你当时在主线程中执行,并且在主线程中已经创建了openFileDialog1?
-
也许你的项目有不止一个
Main方法和另一个你认为使用的方法?这里使用的是MSI_Comparison_GUI.Program.Main()in c:\tfs\DocuWare .NET\DocuWare NewGen\src\Tools\MSI_Comparison\MSI_Comparison_GUI\Program.cs:line 18。 -
@Adkins 如果是这种情况,您的最后一个选择是尝试使用尽可能少的代码在另一种解决方案中重现此问题。如果可以,请打开Connect w/MS。在这里发布addy,我会赞成它。如果您不能重现,则必须逐行、逐个类、逐个项目文件地梳理这两个项目,看看两者之间有什么不同。抱歉,我无法提供更多帮助。
标签: c#