首先 创建 等待窗体 WatingForm:
其次 在WatingForm窗体下编写代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WaitDialog
{
public partial class WaitingForm : Form
{
private static WaitingForm waitForm =null;
public WaitingForm()
{
InitializeComponent();
}
public static void ShowForm(string caption)
{
if (waitForm == null || waitForm.IsDisposed == true)
waitForm = new WaitingForm();
waitForm.Caption.Text = caption;
waitForm.Refresh();
//System.Threading.Thread.Sleep(500);
waitForm.ShowDialog();
}
public static void CloseForm(string caption)
{
if (waitForm != null && waitForm.IsDisposed == false)
{
waitForm.Caption.Text = caption;
waitForm.Refresh();
System.Threading.Thread.Sleep(1000);
waitForm.Dispose();
}
}
public static void SetCaption(string caption)
{
if (waitForm != null && waitForm.IsDisposed == false)
{
waitForm.Caption.Text = caption;
waitForm.Refresh();
}
}
}
}
最后 调用窗体MainParentForm 中添加组件 BackgroundWorker,窗体加载下代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void WaitParentForm_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
WaitDialog.WaitingForm.ShowForm("数据下载中");
}
backgroundWorker相关代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//程序处理,加载数据代码。。。。
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
WaitDialog.WaitingForm.CloseForm("加载完毕");
}
完成!