c# - STAThread 不见了,但它在那里

标签 c#

这是我尝试在我的程序中打开 OpenFileDialog 时收到的错误消息:

"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 exception is only raised if a debugger is attached to the process."

此错误消息的问题在于我的 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());
    }
}

并且没有显式执行线程。老实说,这只是一个非常基本的程序。

编辑2::

这里是完整的错误信息,包括调用栈

System.Threading.ThreadStateException was unhandled Message="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 exception is only raised if a debugger is attached to the process." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner) at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner) at System.Windows.Forms.CommonDialog.ShowDialog() at MSI_Comparison_GUI.Form1.btnOldFind_Click(Object sender, EventArgs e) in c:\tfs\DocuWare .NET\DocuWare NewGen\src\Tools\MSI_Comparison\MSI_Comparison_GUI\Form1.cs:line 70 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at MSI_Comparison_GUI.Program.Main() in c:\tfs\DocuWare .NET\DocuWare NewGen\src\Tools\MSI_Comparison\MSI_Comparison_GUI\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

最佳答案

这可能是您在 Connect1 上报告的以下问题:

.vshost.exe forces wrong threading model used when debugging a .exe if a .dll of the same name exists in same bin directory

根据该问题,当您同时拥有 myprogram.exemyprogram 时,Visual Studio 的托管进程即 myprogram.vshost.exe 会强制执行错误的单元状态.dll 文件在您的输出文件夹中。

这个问题可能特定于某些旧版本的 Visual Studio (2005),我无法使用 VS 2010 重现它。

明显的解决方法是更改​​ dll 的名称或将 dll 移动到另一个文件夹。

出现这种情况可能是因为您将项目的输出类型从类库更改为 Windows 应用程序。

1目前不清楚这个问题是否得到了微软的确认,只是说这个问题不在VS产品团队的责任范围内。

关于c# - STAThread 不见了,但它在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3584434/

相关文章:

c# - 检测单引号内的两个连续单引号

c# - linq 加入 guid 和字符串列

c# - 为什么我可以安全地转换为 int 而不能转换为 int?

c# - 以编程方式在文本框中移动插入符,排列和排列

c# - DotNetZip 间歇性挂起

c# - C#发送带附件的邮件

c# - Double 到 String 的转换而不丢失指数部分

c# - 我可以在 AutoMapper 中集中我想忽略的映射吗?

c# - 如何避免 DataView.ToTable() 中出现 KeyNotFoundException?

c# - 如何在 C# 中更新查询字符串?