Winform中的ThreadStateException,尤其是在OpenFileDialog中

时间:2015-10-06 19:29:07

标签: c# winforms

根据MSDN,ThreadStateException的发生主要是因为新的Thread在主线程中创建了OpenFileDialog的工具;

private void button_FileExercise_Click(object sender, EventArgs e)
{
    OpenFileDialog fileDialog = new OpenFileDialog();
    fileDialog.InitialDirectory = "C:\\";
    fileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
    if (fileDialog.ShowDialog() == DialogResult.OK)
    {
    }
}

ThreadStateException发生在“if”,因此,我如何解决类似的程序,THX!

1 个答案:

答案 0 :(得分:0)

如果没有设置 [STAThread] ,您可能需要通过Thread-Apartment-Safe来实现单线程

This link explains how