有時候我們需要彈出個提示框然后讓它自己關閉,然而實際使用中的彈出框確實阻塞進程,網上貌似有一種另類的解決方式,大致思路是把彈出框放到另外的一個窗體上,直接貼代碼
主窗體
using System; using System.Windows.Forms; namespace WinForm { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //延遲800毫秒關閉的信息框 MessageBox.Show(new DelayCloseForm(800), "執行成功!"); } } }
起到延遲作用的窗體
using System; using System.ComponentModel; using System.Windows.Forms; namespace WinForm { public partial class DelayCloseForm : Form { public DelayCloseForm(int interval = 500) { InitializeComponent(); //計時器 this.components = new Container(); Timer timer1 = new Timer(this.components); timer1.Enabled = true; timer1.Interval = interval; timer1.Tick += timer1_Tick; timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { this.Close(); } } }