0

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

此表单用于获取所需的文件夹位置,以用作安装部分的位置。表格很好,如果我手动打入文件夹位置,它就可以工作。但是哪个用户想要手动执行此操作呢?所以我添加了一个按钮,该按钮应该用作文件夹对话框,但是,当单击它时,我得到一个线程状态异常。

在研究了这个问题之后,发生这种情况的最常见原因是因为您在程序的 main 方法上缺少 [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)

非常感谢任何关于我做错了什么的帮助或建议。谢谢你。

4

1 回答 1

1

在 System.ComponentModel.BackgroundWorker.WorkerThreadStart(对象参数)

有你的问题。

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

于 2012-12-17T15:28:25.793 回答