gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:04:55 25 4
gpt4 key购买 nike

这是我尝试在我的程序中打开 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com