我在ASP.NET上很新,但我已经在桌面应用程序中使用了BackgroundWorkers。
这次我创建了一个显示按钮的简单ASP页面。当它被点击时,会调用一个5秒的操作,我想设置一个图形" loading"州。例如,我只想禁用该按钮。
我试过这种方式:
private BackgroundWorker worker = new BackgroundWorker();
// ...
protected void confirmButton_Click(object sender, EventArgs e)
{
if (selectedAccount == null || worker.IsBusy) return;
worker = new BackgroundWorker();
worker.WorkerReportsProgress = false;
worker.WorkerSupportsCancellation = false;
worker.DoWork += (ss, ee) => {
// the operation
sync.StartOperation(selectedAccount);
};
worker.RunWorkerCompleted += (ss, ee) =>
{
// Operation finished, update the GUI
Report finalReport = sync.GetFinalReport();
if (finalReport.HasErrors())
{
ShowErrorMessage("Error");
}
else
{
ShowSuccessMessage("Completed");
}
SetLoadingState(false);
};
// before starting the worker, set a "loading" status ( => disable the button)
SetLoadingState(true);
// then start!
worker.RunWorkerAsync();
}
这里有两个问题:
worker.isBusy
值...但此属性始终为false ,即使操作明显正在运行对于记录,应该调用RunWorkerCompleted的最后一部分。使用错误或成功消息正确更新GUI。
这有什么不对?感谢。