c#显示对话框线程

本文关键字:线程 对话框 显示 | 更新日期: 2023-09-27 17:50:31

我有一个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();
    }

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

c#显示对话框线程

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

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

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

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