ShowDialog没有阻止执行代码,但阻止用户界面
问题描述:
当我使用ShowDialog()显示表单时,它阻止用户界面和代码,但我只需要阻止用户界面不代码。ShowDialog没有阻止执行代码,但阻止用户界面
letturalog can3 = new letturalog();
(new System.Threading.Thread(() => {
can3.ShowDialog();
})).Start();
此模式不会阻止代码和用户界面。
所以我,想知道如果你能做到这一点
答
如果你不想阻止代码,那么你要调用.Show
换句话说,你想:
can3.Show(this);
this.Enabled = false; //disable the form so the UI is blocked
//...do our stuff now that code is not blocked while the UI is blocked
//All done processing; unblock the UI:
this.Enabled = true;
其实这就是ShowDialog
所做的一切:禁用表单,然后重新启用它。在伪代码:
void ShowDialog(IWindowHandle Owner)
{
this.Show(Owner);
try
{
//Disable the owner form
EnableWindow(Owner, false);
repeat
{
Application.DoEvents();
}
until (this.DialogResult != DialogResult.None);
}
finally
{
//Re-enable the UI!
EnableWindow(owner, true);
}
}
您可以窃取所有这些概念,并替换为任何你想要的胆量:
void DoStuffWithTheThing()
{
can3.Show();
try
{
//Disable the owner form
this.Enabled = false;
//todo: Solve the P=NP conjecture
}
finally
{
//Re-enable the UI!
this.Enabled = true;
}
}
如果您不希望阻止该代码是在另一个线程,你已经做它。显示对话框只会阻止它被调用的线程。 – Ian
是的,但我写了34.000行代码..它不可能改变所有的showdialog ... – user3477026
ShowDialog()通过禁用应用程序中的所有窗口,然后在循环中调用DoEvents(),直到DialogResult属性被分配,然后重新启用所有窗口。所以你得到相同的结果,减去“阻止代码”,使用Show()并禁用所有窗口。 –