“在进行OLE调用之前,当前线程必须设置为单线程公寓(STA)模式。请确保您的Main函数已标记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());
}
}
并没有显式地进行线程处理。说实话,这只是一个相当基本的程序。
编辑2:
下面是完整的错误消息,包括调用堆栈:
System.Threading.ThreadStateException未处理 Message="当前线程必须设置为单线程公寓(STA)模式,才能进行OLE调用。请确保Main函数上标记了STAThreadAttribute。如果调试器连接到进程,则会引发此异常。" 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:
Main
方法,而你认为被使用的可能不是那个?这里使用的是MSI_Comparison_GUI.Program.Main()
,位于c:\tfs\DocuWare .NET\DocuWare NewGen\src\Tools\MSI_Comparison\MSI_Comparison_GUI\Program.cs的第18行。 - Dirk Vollmar