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;
}