wiseyu
一个默默无闻的搬运工

显示效果:

FrmWaiting.cs:

 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         }
View Code

Form1.cs:

 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         }
View Code

源代码下载:FrmWaiting

 第二种实时显示进度:

源代码:代码下载

第二种源博客地址:https://www.cnblogs.com/lijuanfei/p/6080594.html

posted on 2018-10-15 17:25  wiseyu  阅读(2760)  评论(1编辑  收藏  举报

Top