C#显示对话框线程

我有一个Dialog Box (导入程序),我用它来选择我要导入到应用程序的文件。 此Dialog Box (导入程序)还有另一个对话框(文件),它是一个OpenFileDialog

代码运行这样的东西

 //Main File if (Importer.ShowDialog == DialogResult.Ok){ // Start Import } //Importer File OnDoubleClick of TextBox if(File.ShowDialog == DialogResult.Ok){ // Find File } 

但是在第二个ShowDialog我总是得到以下错误:

 An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll Additional information: 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. 

这是一个线程问题,我应该如何处理它。

我期待着您的回音。 问候詹姆斯

– 更新一些代码以帮助这一切都在第一个Form.ShowDialog()中

 private void fileNametxt_DoubleClick(object sender, EventArgs e) { myth = new Thread(new System.Threading.ThreadStart(ChooseFile)); myth.ApartmentState = ApartmentState.STA; myth.Start(); while(myth.ThreadState != ThreadState.Aborted || myth.ThreadState != ThreadState.Stopped) { fileNametxt.Text = FileName; } fileNametxt.Text = FileName; } private void ChooseFile() { openFileDialog.ShowDialog(); if (openFileDialog.FileName != "") { FileName = openFileDialog.FileName.Trim(); } myth.Abort(); } 

如何停止线程并更新屏幕上的文本。 该线程似乎只能与varibles交谈而不是UI控件。

要修复它,请使用属性[STAThread]标记Program类的Main()方法。

后续阅读: CA2232:使用STAThread标记Windows窗体入口点 。

在这种情况下,当然Main()方法您的入口点,无论框架是什么。

在这个错误中。 构建解决方案不会引发错误,但调试会引发该错误。 我尝试创建新的解决方案并为新解决方案添加令人兴奋的项 错误已修复!