显示效果:
FrmWaiting.cs:
public FrmWaitingBox(EventHandler<EventArgs> Method,string msg)
{
InitializeComponent();
if (!string.IsNullOrEmpty(msg))
lblTip.Text = msg;
_Method = Method;
}
private EventHandler<EventArgs> _Method;
private IAsyncResult asyncResult;
private void timer1_Tick(object sender, EventArgs e)
{
if (asyncResult.IsCompleted)
this.Close();
}
/// <summary>
/// 窗体第一次加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmWaitingBox_Shown(object sender, EventArgs e)
{
asyncResult = _Method.BeginInvoke(null, null, null,null);
}
Form1.cs:
private void btnStart_Click(object sender, EventArgs e)
{
var list = new List<User>();
FrmWaitingBox waitingBox = new FrmWaitingBox((obj, args) =>
{
//Thread.Sleep(5000);
for (int i = ; i <= ; i++)
{
list.Add(new User {
Id=i,
UserName="Test"+i,
Age=+i,
Birth=DateTime.Now
});
}
//this.Invoke(new Action(() =>
//{
// dgvData.DataSource = list;
//}));
},"数据获取中,请等待!");
waitingBox.ShowDialog(this);
dgvData.DataSource = list;
}
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
public int Age { get; set; }
public DateTime Birth { get; set; }
}
源代码下载:FrmWaiting
第二种实时显示进度:
源代码:代码下载