显示效果:
FrmWaiting.cs:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public FrmWaitingBox(EventHandler<EventArgs> Method,string msg) 2 { 3 InitializeComponent(); 4 if (!string.IsNullOrEmpty(msg)) 5 lblTip.Text = msg; 6 _Method = Method; 7 } 8 private EventHandler<EventArgs> _Method; 9 private IAsyncResult asyncResult; 10 private void timer1_Tick(object sender, EventArgs e) 11 { 12 if (asyncResult.IsCompleted) 13 this.Close(); 14 } 15 /// <summary> 16 /// 窗体第一次加载事件 17 /// </summary> 18 /// <param name="sender"></param> 19 /// <param name="e"></param> 20 private void FrmWaitingBox_Shown(object sender, EventArgs e) 21 { 22 asyncResult = _Method.BeginInvoke(null, null, null,null); 23 }
Form1.cs:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 private void btnStart_Click(object sender, EventArgs e) 2 { 3 var list = new List<User>(); 4 FrmWaitingBox waitingBox = new FrmWaitingBox((obj, args) => 5 { 6 //Thread.Sleep(5000); 7 for (int i = 1; i <= 50; i++) 8 { 9 list.Add(new User { 10 Id=i, 11 UserName="Test"+i, 12 Age=18+i, 13 Birth=DateTime.Now 14 }); 15 } 16 //this.Invoke(new Action(() => 17 //{ 18 // dgvData.DataSource = list; 19 //})); 20 },"数据获取中,请等待!"); 21 waitingBox.ShowDialog(this); 22 dgvData.DataSource = list; 23 } 24 public class User 25 { 26 public int Id { get; set; } 27 public string UserName { get; set; } 28 public int Age { get; set; } 29 public DateTime Birth { get; set; } 30 }
源代码下载:FrmWaiting
第二种实时显示进度:
源代码:代码下载