可以处理进度,完成通知
mworker = new BackgroundWorker();
mworker.DoWork += new DoWorkEventHandler(OnWork);
mworker.WorkerReportsProgress = true;
mworker.ProgressChanged += new ProgressChangedEventHandler(OnWorkProgressChanged);
mworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnWorkCompleted);
mworker.WorkerSupportsCancellation = true;
mworker.RunWorkerAsync();
在 OnWork 的实现
void OnWork(object sender, DoWorkEventArgs e)
{
while (true)
{
// …
if (…)
break;// …
}
}
在 Complete 时,应该判断多种结束情况
void OnMeasureCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
}
else if (e.Cancelled)
{
}
else
{
}
}
-