2012-10-31 30 views
0

有没有办法在后台线程中显示表单,但是您可以在UI线程之前始终将其视为无模式?如何在UI线程无模式的背景线程中显示Dialog?

例如,UI线程与parentForm一起运行,并且有一个backgroundworker线程在顶部与childForm一起运行。我可以使用带有childForm的parentForm放在无模式上,这意味着我总是可以看到我的childForm,但不会阻止我的parentForm。

看来childForm.ShowDialog(parentForm)会阻塞UI线程,我不想在UI线程中调用childForm。

+1

可以显示()的形式和最顶层设置为true。 – Blorgbeard

+0

谢谢,但它会顶部所有的窗口不仅主要的UI线程? –

回答

1

我不知道你是什么意思,但你总是可以尝试一个特定的形式中运行Show(),如果你想显示的形式,而不会阻塞主UI

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);  
    } 
} 

谢谢,
我希望对您有所帮助:)

+0

抱歉没有说清楚。两个线程都获得了顶部的表单,并且我想从主UI线程顶部的背景创建一个表单。 –