当[STAThread]已经列在main()上面时处理threadstateexception

时间:2012-12-17 15:20:19

标签: c# winforms multithreading exception

我有一个安装程序,我正在尝试添加一个在安装过程中达到某个点时弹出的表单。

此表单用于获取所需的文件夹位置,以用作安装部分的位置。表格很好,如果我手动打卡在文件夹位置,它的工作原理。但是用户想要手动执行此操作的是什么?所以我添加了一个按钮,假设它充当文件夹对话框,但是当点击它时我得到一个threadstateexception。

在研究这个问题之后,最常见的原因是因为你错过了[stathread]而不是程序的主要方法。这已经添加到我的安装程序中,所以我希望有人能让我知道我到底做错了什么,因为winforms不是我花了很多时间工作的东西。

这是我正在使用的这个特殊问题的代码。

打开表单的电话。

OracleDriveLocations ODL = new OracleDriveLocations();
ODL.ShowDialog();

文件夹对话框按钮的代码。

this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
this.folderBrowserDialog1.ShowNewFolderButton = true;
DialogResult rst = this.folderBrowserDialog1.ShowDialog();
if (rst == DialogResult.OK)
{
  string path = this.folderBrowserDialog1.SelectedPath;
  logger.DebugFormat("Data Files path was changed to {0}.", path);
  this.DataFileLocationTB.Text = path;
}

我也尝试过使用临时线程来运行它,但无效:

var t = new Thread((ThreadStart)(() => 
{
                this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
                this.folderBrowserDialog1.ShowNewFolderButton = true;
                DialogResult rst = this.folderBrowserDialog1.ShowDialog();
                if (rst == DialogResult.OK)
                {
                    string path = this.folderBrowserDialog1.SelectedPath;
                    logger.DebugFormat("Data Files path was changed to {0}.", path);
                    this.DataFileLocationTB.Text = path;
                }
}));

t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();

请求的堆栈跟踪

         at System.Windows.Forms.FolderBrowserDialog.RunDialog(IntPtr hWndOwner)    
    at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)   
    at DM.Installer.OracleDriveLocations.DataFilesButton_Click(Object sender, EventArgs e) in C:\Users\Administrator\Desktop\PIDirect_Access\Installer\PIDirectAccessInstaller64Bit
        - try 2\DM.Installer\OracleDriveLocations.cs:line 51   
 at System.Windows.Forms.Control.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.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.Form.ShowDialog(IWin32Window owner)    
    at DM.Installer.SchemaHandler.Install(BackgroundWorker worker, DoWorkEventArgs e, InstallerData data) in C:\Users\Administrator\Desktop\PIDirect_Access\Installer\PIDirectAccessInstaller64Bit
        - try 2\DM.Installer\SchemaHandler.cs:line 100    
    at DM.Installer.InstallProgressPage.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\Administrator\Desktop\PIDirect_Access\Installer\PIDirectAccessInstaller64Bit
        - try 2\DM.Installer\InstallProgressPage.cs:line 404    
    at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

我非常感谢您对我做错的任何帮助或建议。谢谢。

1 个答案:

答案 0 :(得分:1)

  

at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

有你的问题。

此代码在BackgroundWorker线程上运行,该线程从不是STA 而是在UI线程上执行此操作。