废话不多说,直接上效果图,代码放在最后
代码展示:
1、新建一个Form窗体,窗体上添加一个button按钮
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WaitForm form = new WaitForm();
form.DoWork += new WaitForm.DoWorkEventHandler(form_DoWork);
StartForm(form);
}
void StartForm(WaitForm form)
{
DialogResult result = form.ShowDialog();
if (result == DialogResult.Abort)
{
MessageBox.Show("错误:" + form.Result.Error.Message);
return;
}
MessageBox.Show("成功");
}
void form_DoWork(WaitForm sender, DoWorkEventArgs e)
{
try
{
//这里写请求逻辑
sender.SetProgress("请求中···");
Thread.Sleep(5000);
// sender.SetProgress("测试中···");
//Thread.Sleep(5000);
//throw new Exception("测试");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
2、新建一个名字为WaitForm的窗体,添加一下几个控件
其中只有 labelStatus 和 PB 是程序中用到的 其他的都是为了布局
/// </summary>
/// <summary>
/// 简单的进度窗体
/// </summary>
public partial class WaitForm : Form
{
/// <summary>
/// 不要在后台任务中直接使用 !
/// 传递给后台任务的参数.
/// </summary>
public object Argument { get; set; }
/// <summary>
/// 后台任务的结果.
///也可以检查ShowDialog返回值
///来看看是不是正确完成了.
/// </summary>
public RunWorkerCompletedEventArgs Result { get; private set; }
/// <summary>
/// 缺省状态文本.
/// </summary>
public string DefaultStatusText { get; set; }
/// <summary>
/// DoWork事件的委托.
/// </summary>
/// <param name="sender">T事件源.</param>
/// <param name="e">包含事件数据.</param>
public delegate void DoWorkEventHandler(WaitForm sender, DoWorkEventArgs e);
/// <summary>
/// 当后台任务开始的时候发生.
/// </summary>
public event DoWorkEventHandler DoWork;
/// <summary>
/// 构造函数.
/// </summary>
public WaitForm()
{
InitializeComponent();
DefaultStatusText = "请稍等...";
Argument = false;
this.PB.Image = RequestWait.Properties.Resources.GIF;
//隐藏窗体
// this.BackColor = Color.White;
// this.TransparencyKey = Color.White;
this.Opacity = 0.85;
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
/// <summary>
/// 改变状态文本.
/// </summary>
/// <param name="status">新状态文本.</param>
public void SetProgress(string status)
{
//如果没有改变
//或者取消请求还在处理就不改变状态文本
if (status != lastStatus && !worker.CancellationPending)
{
lastStatus = status;
worker.ReportProgress(0, status);
}
}
private void WaitForm_Load(object sender, EventArgs e)
{
//重用窗体,恢复缺省值
Result = null;
labelStatus.Text = DefaultStatusText;
lastStatus = DefaultStatusText;
//窗体已载入就开始后台任务
worker.RunWorkerAsync(Argument);
//默认触发一次
SetProgress(DefaultStatusText);
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//如果取消请求正在处理就不要更新
if (e.UserState != null && !worker.CancellationPending)
labelStatus.Text = e.UserState.ToString();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
//后台任务开始
//调用用户的处理程序
if (DoWork != null)
DoWork(this, e);
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//后台任务完成
//保持结果,关闭窗体
Result = e;
if (e.Error != null)
DialogResult = DialogResult.Abort;
else if (e.Cancelled)
DialogResult = DialogResult.Cancel;
else
DialogResult = DialogResult.OK;
Close();
}
BackgroundWorker worker;
string lastStatus;
}