如何在UI线程无模式的后台线程中显示Dialog?

时间:2012-10-31 04:29:48

标签: c# winforms

有没有办法在后台线程中显示表单但是你总是可以在UI线程面前看到它是无模式的?

例如,UI线程使用parentForm运行,并且有一个backFree运行带有childForm的后台工作线程。我可以使用带有childForm的parentForm在无模式下工作,这意味着我总能看到我的childForm但不能阻止我的parentForm。

似乎childForm.ShowDialog(parentForm)将阻止UI线程,我不想在UI线程中调用childForm。

1 个答案:

答案 0 :(得分:1)

我不确定你是什么意思,但是如果你想在不阻挡主用户界面的情况下显示表单,你总是可以尝试在特定表单中运行Show()

示例

Form2 _Form2 = new Form2();
_Form2.Show();

或者,如果您想以异步方式运行新表单作为应用程序的主要表单,您可以尝试创建新的Thread并在其中运行表单

示例

public void RunThread()
{
    Thread thread = new Thread(new ThreadStart(RunForm)); //Create a new thread to execute RunForm()
    thread.Name = "NewForm"; //Name the new thread (Not required)
    thread.Start(); //Start executing RunForm() in the new thread
}

public void RunForm()
{
    try
    {
        Application.Run(new Form2()); //Run Form2() as the main form of the application
    }
    catch (Exception ex)
    {
        //DoSomething
        //MessageBox.Show(ex.Message);       
    }
}

谢谢, 我希望你觉得这很有帮助:)