Winform 彈出窗體等待


顯示效果:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM