以下是I类工作的缩减版本(WinForms项目的一部分):
class ReportBuilder {
private List<Project> projects;
private List<Invoice> invoices;
private MyAPI apiObject;
public ReportBuilder(MyAPI apiAccess, List<Project> selectedProjects){
this.apiObject = apiAccess;
this.projects = selectedProjects;
}
public void DownloadData(){
BackgroundWorker workerThread = new BackgroundWorker();
workerThread.DoWork += (sender, e) => this.retrieveInvoices(this.projects); // yes, the parameter is unnecessary in this case, since the variable is in scope for the method anyway, but I'm doing it for illustrative purposes
workerThread.RunWorkerCompleted += receiveData;
workerThread.RunWorkerAsync();
}
private void retrieveInvoices(List<Project> filterProjects){
Notification status;
if (filterProjects == null){this.invoices = this.apiObject.GetInvoices(out status);}
else {this.invoices = this.apiObject.GetInvoices(filterProjects, out status);}
}
private void receiveData(Object sender, RunWorkerCompletedEventArgs e){
// display a save file dialog to the user
// call a method in another class to create a report in csv format
// save that csv to file
// ... ideally, this method would to have access to the 'status' Notification object from retrieveInvoices, but it doesn't (unless I make that an instance variable)
}
}
现在,通常DoWork事件处理程序的方法签名是这样的:
private void retrieveInvoices(object sender, DoWorkEventArgs e)
但是,正如您在上面所看到的,我的retrieveInvoices方法的签名并不匹配。因此我预计它会失败(要么不编译,要么只在UI线程上运行retrieveInvoices,阻止它,而不是在后台工作程序中)。令我惊讶的是,它似乎正在起作用,但由于我见过的所有BackgroundWorker示例都没有这样做,我仍然认为我必须做错事。但是我,为什么?
答案 0 :(得分:6)
该行:
worker.DoWork += (sender, e) => this.retrieveInvoices(this.projects);
引入一个参数(object sender, DoWorkEventArgs e)
的委托,该参数使用参数retrieveInvoices
调用方法projects
。没有语法不匹配。
这相当于:
worker.DoWork += (sender, e) => { this.retrieveInvoices(this.projects); }
或
void doSomething(object sender, System.ComponentModel.DoWorkEventArgs e)
{
this.retrieveInvoices(this.projects);
}
worker.DoWork += doSomething;
要使用retrieveInvoices
作为实际的事件处理程序,您必须写:
worker.DoWork += retrieveInvoices;
会导致不匹配。
BTW BackgroundWorker已经过时了。使用Task.Run,async / await和IProgress可以完成它所做的任何事情。例如,BGW不能用于组合多个异步操作。 `async / await同样容易,例如:
async Task<Report> RunReport(Project[] projects, IProgress<string> progress)
{
var data= await retrieveInvoices(projects);
progress.Report("Invoices retrieved");
var report=await render(data);
progress.Report("Report rendered");
await SaveReport(report);
progress.Report("Report saved");
return report;
}
//...
Progress<string> progress=new Progress<string>(msg=>statusBar1.Text=msg);
await RunReport(projects);