C#显示对话框线程

C#显示对话框线程,c#,multithreading,dialog,showdialog,C#,Multithreading,Dialog,Showdialog,我有一个对话框(导入器),用于选择要导入应用程序的文件。此对话框(导入器)还有另一个对话框(文件),它是一个OpenFileDialog 代码运行如下所示 //Main File if (Importer.ShowDialog == DialogResult.Ok){ // Start Import } //Importer File OnDoubleClick of TextBox if(File.ShowDialog == DialogResult.Ok){ // Find File

我有一个
对话框
(导入器),用于选择要导入应用程序的文件。此
对话框
(导入器)还有另一个对话框(文件),它是一个
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.
这是一个线程问题,我应该如何处理它

我期待着收到你的来信。 当做 詹姆斯

--更新更多代码以帮助实现这一点,这些都在第一个表单中


如何停止线程并更新屏幕上的文本。线程似乎只能与变量对话,而不能与UI控件对话。

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

后续阅读:


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

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

后续阅读:


在这种情况下,当然
Main()。生成解决方案不是引发错误,而是调试引发该错误。

我尝试创建新的解决方案,并在新的解决方案中添加令人兴奋的项目。错误已修复

在这个错误中。生成解决方案不是引发错误,而是调试引发该错误。
我尝试创建新的解决方案,并在新的解决方案中添加令人兴奋的项目。错误已修复

检查。对话框是从主线程显示的吗?程序员有一个反向操作的诀窍。正确的方法是在UI线程上显示对话框,并在工作线程上运行导入代码。反过来做总是错误的,并且会产生像这样的运行时异常。还有一个大麻烦。使用BackgroundWorker或Task类来运行导入程序。您还应该包含其余代码。这并没有提供足够的线索来理解导致交叉线程问题的原因。为什么选择将对话框显示在远离主线程的位置?请检查。对话框是从主线程显示的吗?程序员有反向显示的诀窍。正确的方法是在UI线程上显示对话框,并在工作线程上运行导入代码。反过来做总是错误的,并且会产生像这样的运行时异常。还有一个大麻烦。使用BackgroundWorker或Task类来运行导入程序。您还应该包含其余代码。这没有提供足够的线索来理解导致交叉线程问题的原因。为什么选择将对话框显示在远离主线程的位置?嗨@Migol,你知道它与web窗体的关系吗?嗨@Migol,你知道它与web窗体的关系吗?
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();

    }