方式1 //阻塞UI线程及消息循环 AutoResetEvent autoResetEvent = new AutoResetEvent(false); Task.Factory.StartNew(() => { Form form = new Form(); form.TopLevel = true; form.TopMost = true; form.Show(); // MessageBox.Show("MessageBox显示窗口在最上层了吗?", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); Thread.Sleep(10000); form.Close(); autoResetEvent.Set(); }); autoResetEvent.WaitOne(); 方式2(可以不容UI继续操作,UI消息循环还不阻塞) //自定义对话框(showdialog不阻塞UI线程和消息循环只是不允许界面操作) class MessageShow { static Form_Message form = null; ........................ } //异步逻辑处创建并显示 MessageShow.form.ShowDialog(parent); //主界面关闭 MessageShow. form.Close(); 方式3 Task.Factory.StartNew(() => { Task.Factory.StartNew(()=>{ Form form = new Form(); form.TopLevel = true; form.TopMost = true; form.ShowDialog(); }); Thread.Sleep(10000); ...............................//找到Form句柄然后关闭 form.Close(); });
方式4
public delegate DialogResult InvokeDelegate(Form parent); int a = 0; public DialogResult MyShowDialog(Form parent) { if (parent.InvokeRequired) { a++; InvokeDelegate myShow = new InvokeDelegate(MyShowDialog); object ob= parent.Invoke(xShowmyShow, new object[] { parent }); DialogResult res = (DialogResult)(ob); return res; } return this.ShowDialog(parent); }
MessageBox.Show(this, "..", "..", MessageBoxButtons.OK, MessageBoxIcon.Information);