STAThread丢失,但它的存在 [英] STAThread missing, but it is there

查看:99
本文介绍了STAThread丢失,但它的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是当我尝试在我的程序打开一个OpenFileDialog,我recieving错误信息:


  

当前线程必须设置为单
  OLE之前线程单元(STA)模式
  呼叫可。确保您的
  主要功能有请将STAThreadAttribute
  标示。此异常仅
  如果一个调试器附加到募集
  的过程。


此错误消息的问题是,我的主要方法确实有它相连的STAThread属性。我在一个不知如何处理这个问题。如何添加的东西,如果它已经存在。加倍它是不是一个好的选择,我试图删除它,构建应用程序,添加它并没有成功再次构建它。我只是不明白。

 私人无效btnOldFind_Click(对象发件人,EventArgs的发送)
{
     openFileDialog1.Multiselect = FALSE;
     openFileDialog1.FileName =;
     openFileDialog1.ShowHelp = FALSE;
     openFileDialog1.AutoUpgradeEnabled = TRUE;
     openFileDialog1.InitialDirectory = @C:\\;
     openFileDialog1.Filter =Microsoft安装程序(* .msi)| * .msi程序|所有文件(*。*)| *。*;
     openFileDialog1.FilterIndex = 1;
     openFileDialog1.RestoreDirectory = TRUE;     如果(openFileDialog1.ShowDialog()== DialogResult.OK)
     {
         textBoxOldInstallation.Text = openFileDialog1.FileName;
     }
}

和主要的方法是:

 静态类节目
{
    ///<总结>
    ///的主入口点应用程序。
    ///< /总结>
    [STAThread]
    静态无效的主要()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(假);
        Application.Run(新Form1中());
    }
}

和没有线程明确完成。只需pretty基本程序是诚实的。

EDIT2:

下面是包括调用栈完整的错误消息


  

System.Threading.ThreadStateException了未处理
    消息=当前线程必须设置为单线程单元(STA)模式下可以进行OLE调用之前,请确保您的主要功能有请将STAThreadAttribute标记就可以了。如果一个调试器附加到进程此异常,才会引发。
    来源=System.Windows.Forms的
    堆栈跟踪:
         在System.Windows.Forms.FileDialog.RunDialog(IntPtr的hWndOwner)
         在System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window所有者)
         在System.Windows.Forms.CommonDialog.ShowDialog()
         在MSI_Comparison_GUI.Form1.btnOldFind_Click(对象发件人,EventArgs e)在C:\\ TFS \\ DocuWare .NET \\ DocuWare的NewGen的\\ src \\ TOOLS \\ MSI_Comparison \\ MSI_Comparison_GUI \\ Form1.cs中:行70
         在System.Windows.Forms.Control.OnClick(EventArgs的发送)
         在System.Windows.Forms.Button.OnClick(EventArgs的发送)
         在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
         在System.Windows.Forms.Control.WmMouseUp(消息和M,MouseButtons按钮,点击的Int32)
         在System.Windows.Forms.Control.WndProc(消息&放大器;米)
         在System.Windows.Forms.ButtonBase.WndProc(消息&放大器;米)
         在System.Windows.Forms.Button.WndProc(消息&放大器;米)
         在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&放大器;米)
         在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&放大器;米)
         在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,LPARAM的IntPtr)
         在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&安培; MSG)
         在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)
         在MSI_Comparison_GUI.Program.Main()在C:\\ TFS \\ DocuWare .NET \\ DocuWare的NewGen的\\ src \\ TOOLS \\ MSI_Comparison \\ MSI_Comparison_GUI \\的Program.cs:行18
         在System.AppDomain._nExecuteAssembly(议会会议,字串[] args)
         在System.AppDomain.ExecuteAssembly(字符串assemblyFile,证据assemblySecurity,字串[] args)
         在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
         在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
         在System.Threading.ThreadHelper.ThreadStart()
    的InnerException:



解决方案

这可能是你正面临汇报连接下列问题 1


  

<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/details/108261/vshost-exe-forces-wrong-threading-model-used-when-debugging-a-exe-if-a-dll-of-the-same-name-exists-in-same-bin-directory\">.vshost.exe力量不对线程模型调试.exe文件时,如果同一个名字的一个.dll同一bin目录中使用


据它发生的Visual Studio宿主进程,即myprogram.vshost.exe强制执行了错误的单元状态,当你同时拥有的 myprogram.exe 的和的 myprogram这个问题.dll文件的文件在你的输出文件夹。

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

最明显的解决办法是改变DLL的名称或将DLL移到另一个文件夹。

这种情况或许有,因为你从类libary改变了你的项目的输出类型Windows应用程序。

1 此问题是否是由Microsoft或不能确认目前还不清楚,它只是说,这个问题是VS产品团队的责任之外。

Here is the error message that I am recieving when I try to open an OpenFileDialog in my program:

"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."

The problem with this error message is that my Main method DOES have the STAThread attribute attached to it. I am at a loss as to how to handle this. How can I add something if it is already there. Doubling it up is not a good option, and I tried erasing it, building the app, adding it and building it again with no success. I just don't understand.

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;
     }
}

and the main method is:

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());
    }
}

And no threading is done explicitly. Just a pretty basic program to be honest.

EDIT2::

Here is the complete error message including call stack

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:

解决方案

It might be that you are facing the following problem reported on Connect1:

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

According to that issue it happens that the hosting process of Visual Studio, i.e. the myprogram.vshost.exe enforces the wrong apartment state when you have both a myprogram.exe and a myprogram.dll file in your output folder.

The problem might be specific to some older version of Visual Studio (2005), and I haven't been able to reproduce it using VS 2010.

The obvious workaround would be to change the name of the dll or to move the dll to another folder.

The situation might have come up because you changed the output type of your project from class libary to Windows application.

1It is unclear whether this problem is confirmed by Microsoft or not, it just says that the problem is outside the responsibility of the VS product team.

这篇关于STAThread丢失,但它的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆