显示效果:
FrmWaiting.cs:
1 public FrmWaitingBox(EventHandlerMethod,string msg) 2 { 3 InitializeComponent(); 4 if (!string.IsNullOrEmpty(msg)) 5 lblTip.Text = msg; 6 _Method = Method; 7 } 8 private EventHandler _Method; 9 private IAsyncResult asyncResult;10 private void timer1_Tick(object sender, EventArgs e)11 {12 if (asyncResult.IsCompleted)13 this.Close();14 }15 /// 16 /// 窗体第一次加载事件17 /// 18 /// 19 /// 20 private void FrmWaitingBox_Shown(object sender, EventArgs e)21 {22 asyncResult = _Method.BeginInvoke(null, null, null,null);23 }
Form1.cs:
1 private void btnStart_Click(object sender, EventArgs e) 2 { 3 var list = new List(); 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.Now14 });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 User25 {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 }
源代码下载:
第二种实时显示进度:
源代码:
第二种源博客地址: