C#学习教程:STAThread不见了,但它在那里共享线程设置为单线程单元(STA)模式。确保在主函数上标记了STAThreadAttribute。仅当调试器附加到进程时才会抛出此异常。”此错误消息的问题是我的Main方法确实附加了STAThread属性。我不知道如何处理这个问题。如何添加一些东西如果它已经存在。加倍它不是一个好的选择,我尝试删除它,构建应用程序,添加它并再次构建它但没有成功。我只是不明白。privatevoidbtnOldFind_Click(objectsender,EventArgse){openFileDialog1.Multiselect=false;openFileDialog1.FileName="";openFileDialog1.ShowHelp=false;openFileDialog1.AutoUpgradeEnabled=true;openFileDialog1.InitialDirectory=@"C:=";"Microsoft安装程序(*.msi)|*.msi|所有文件(*.*)|*.*";openFileDialog1.FilterIndex=1;openFileDialog1.RestoreDirectory=true;if(openFileDialog1.ShowDialog()==DialogResult.OK){textBoxOldInstallation.Text=openFileDialog1.FileName;}}主要方法是:staticclassProgram{//////应用程序的主要入口点。///[STAThread]staticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);应用程序运行(新Form1());}}而不是显式线程化。老实说,这是一个非常基本的计划。EDIT2::这是完整的错误消息,包括调用堆栈System.Threading.ThreadStateExceptionUnhandledMessage="在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。确保您的Main函数标有STAThreadAttribute。仅当调试器附加到进程时。"Source="System.Windows.Forms"StackTrace:System.Windows.Forms.FileDialog.RunDialogatSystem.Windows.Forms.Forms.CommonDialog.ShowDialog(IWin32Windowowner)IntPtrhWndOwner)。)在c:tfsDocuWare.NETDocuWareNewGensrc工具MSI_ComparisonMSI_Comparison_GUIForm1.cs:MSI_Comparison_GUI.Form1.btnOldFind_Click(Objectsender,EventArgse)online70.OnClick(EventArgse)atSystem.Windows.Forms.OnClickatSystem.Windows.Forms.Button.OnClick(EventArgse)(SystemErgse),System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtonsbutton,atSystem.Int32atSystem.Windows.Forms.Control.WndProc(Message&m)at单击Windows上的System.Windows.Forms.Button.WndProc(Message&m)位于System.Windows.Forms.ButtonBase.WndProc(Message&m))。System.Windows.Forms.NativeWindow.Debu的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中的Forms.Control.ControlNativeWindow.OnMessage(Message&m)System.Windows.Forms.Application.ComponentManager.System.Windows。Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop上的System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)中的ggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)系统中的System.Windows.Forms.Application.ThreadContext。RunMessageLoopInner(Int32原因,ApplicationContext上下文)中的System32.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)中的(Int32dwComponentID,Int32原因,Int32pvLoopData).Windows.Forms.Application.Run(FormmainForm)位于c:tfsDocuWare.NETDocuWareNewGensrcToolsMSI_ComparisonMSI_Comparison_GUIProgram.cs:System18中的MSI_Comparison_GUI.Program.Main()。AppDomain._nExecuteAssembly(assembly,String[]args)在System.AppDomain.ExecuteAssembly(StringassemblyFile,EvidenceassemblySecurity,String[]args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()onSystem.Threading.T)hreadHelper。ThreadStart_Context(Objectstate)inSystem.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,objectstate)inSystem.Threading.ThreadHelper.ThreadStart():InnerException:MaybeyouareexperiencedthereportonConnect1以下问题:如果同名的.dll存在于同一个bin目录中,.vshost.exe在调试.exe时强制使用错误的线程模型根据问题,当您在输出文件夹中包含myprogram.exe和myprogram.exe时。dll文件,VisualStudio的托管进程发生,即myprogram.vshost.exe强制执行错误的单元状态。该问题可能特定于某些旧版本的VisualStudio(2005),我无法使用VS2010重现它。明显的修复是更改dll的名称或将dll移动到另一个文件夹。这可能是因为您将项目的输出类型从类库更改为Windows应用程序。1不清楚这个问题是否得到了微软的确认,只说这个问题不在VS产品团队的责任范围内。你有一个不可能的堆栈跟踪。显然线程不是问题的原因,一切都在主线程上运行,Main方法的[STAThread]属性正在设置单元状态。堆栈跟踪显示它确实是入口点。好吧,坏消息,某种附加功能正在针对您的主线程运行。做一些讨厌的事情,比如多次调用CoUninitialize。我遇到过一次,花了一个月的时间才找到它。使用Project+Properties,Debug选项卡开始诊断,勾选“Enableunmanagedcodedebugging”。这允许您查看哪些DLL已加载到您的程序中,它显示在“输出”窗口中。第一次启动是当对话框第一次显示正常但第二次失败时。然后你有一些shell扩展处理程序,它已经进入你的程序。使用SysInternals的AutoRuns实用程序并禁用任何非Microsoft生成的shell扩展处理程序。当对话立即失败时,它会变得更难。然后使用Debug+Windows+Modules并浏览DLL列表。请注意它们的来源,显示在“路径”列中。不要相信任何不会像.NET或MicrosoftDLL这样的东西。启用MicrosoftSymbolServer后,特别是没有符号文件成为主角。与此相关的一个好方法是将该列表与您在另一台没有此问题的计算机上看到的列表进行比较。我确实有一个关于这个的战争故事。我的COM代码在数百台机器上崩溃,我所要做的只是一台mini。我花了一个月的时间才发现一个名为ffdshow的开源项目。分布广泛,使用不同的名称。它有一个错误,调用CoUnitialize两次太多了。该错误在发布时存在了两年,但在一年半前被修复。很难诊断,直到我开始查看旧版本,我才开始接近它。如果您在模块窗口中看到ffdshow,那么您就结束了:)祝您好运,让我们了解邪恶的参与者。没有代码就不能说。如果是控制台应用程序,在调用该方法之前添加以下内容:Console.Write(System.Threading.Thread.CurrentThread.ApartmentState);否则,MessageBox.Show(System.Threading.Thread.CurrentThread.ApartmentState);看看线程单元状态到底是什么。尝试在主进程中设置断点,创建对话框的位置以及使用它的位置。然后查看您实际所在的线程。此外,在这些点检查Thread.CurrentThread.GetApartmentState()的值。以上就是C#学习教程:缺少STAThread,不过是里面分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处:
