我有一个对象列表,我想运行一个后台工作进程来处理列表中的每个对象。 BWP应该禁用UI,完成其操作,并且onComplete,应该再次启用UI。
我试图在foreach循环中调用BGW,并将列表也传递给RunAsyn。但是它似乎没有用。预期的行为应该是BWP应该一次选择一个项目并完成所有操作(通常每个对象操作大约1分钟)以启用UI,然后移至对象列表中的下一个项目。
到目前为止,我已经尝试过
public void CalculateAll()
{
using (BackgroundWorker backgroundWorker = new BackgroundWorker())
{
backgroundWorker.DoWork += bwCalculate_DoWork;
backgroundWorker.RunWorkerCompleted += bw_RunWorkerCompleted;
// Start the background worker
backgroundWorker.RunWorkerAsync(objList);
}
}
private void bwCalculate_DoWork(object sender, DoWorkEventArgs e)
{
var objList = e.Argument as List<objects>;
var worker = sender as BackgroundWorker;
foreach (var obj in objList )
{
//disable UI (buttons etc)
//process the obj
//wait for BWP to finish
//move to next item
}
}
private void bw_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
//enable UI
}