使用BackgroundWorker组件
BackgroundWorker 组件用来执行诸如数据库事务、文件下载等耗时的异步操作。
开始
在应用程序 中添加一个BackgroundWorker实例,如果用的是VS,可以从工具上直接拖到应用程序:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
为了开始在后台操作,必须调用BackgroundWorker的 RunWorkerAsync()方法,当调用此方时,BackgroundWorker 通过触发DoWork 事件,开始执行后台操作,DoWork 事件的代码是在另一个线程里执行的。代码如下:
代码一:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
//在另一个线程里开始操作(btnStart是一个按钮控件)
//也可以利用 RunWokerAsync()方法传递参数,
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWokerAsync(2000/*参数是可选的*/);
}
//DoWork 事件在另一个线程里执行
private void backgroundWorker1_DoWork(objectsender,DoWorkeventArgs e)
{
//允许长时间的操作
int input = (int)e.Argument;
Thread.Sleep(input);
}
显示后台操作进度
为了显 示后台操作的执行进度,首先要使WorkerReportsProgress 等于true,然后调用BackgroundWorker的ReportProgress()方法,通过它传递操作完成的进度值,此 外,该方法触发ProgressChanged事件,在是此事件中,通过ProgressChangedEventArgs的实例,接收到主线程传递过来 的参数。
代码二:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWokerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
{
for (int i = 1; i < 11; i++)
{
Thread.Sleep(2000);
backgroundWorker1.ReportProgress(i*10);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//progressBar1是ProgressBar控件
progressBar1.Value = e.ProgressPercentage;
}
取 消后台操作
为了使 BackgroundWorker 可以取消后台正在执行的操作,首先要把属性WorkerSupportsCancellation 的值设置为 true。接着调用CancelAsync()方法,该方法使得属性CancellationPending 为true,利用CancellationPending 属性,可以判断是否取消后台异步操作。
代码三:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWokerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
{
for (int i = 1; i < 11; i++)
{
Thread.Sleep(2000);
backgroundWorker1.ReportProgress(i*10);
if(backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
后台操作完成后,反馈给用户
当后台操作完成以后,无论是completed 还是cancelled,则RunWorkerCompleted 事件被触发,通过此方法可以将后台操作的完成结果反馈给用户;
另外, 通过RunWorkerCompletedEventArgs实例的Cancelled 属性,以判断是否是Cancel操作使得后台操作终止;
代码片段:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Cancelled)
{
MessageBox.Show("Operation Cancelled");
}
else
{
MessageBox.Show("OperationCompleted");
}
}
从后台操作返回值
在执行 DoWork 事件时,DoWorkEventArgs 实例的Result 属性,返回值到用户;在RunWorkerCompleted 事件里,RunWorkerCompletedEventArgs 实例的Result 属性接收值;
代码片段:
private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
{
Thread.Sleep(2000);
//在此处设置返回值
e.Result = 10;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// 在此处接收传递回来的值
int returnValue = (int)e.Result;
}
结束
![](http://img.zemanta.com/pixy.gif?x-id=35c0b349-49a1-8dfb-b29e-55f44b711b94)