我正在使用进度条向用户显示耗时操作的进度。我的问题是,在执行操作时,背景工作人员无法报告任何进度,直到它完成了。因此,如果我只有一次迭代,进度条总是显示0%,并且完成后,进度条会立即关闭。因此,用户不会感觉到任务已完成。我正在寻找一种方法让用户在progessbar关闭之前知道任务已完成。我的代码如下所示。如何在关闭前100%显示进度条
this.progressDialog = new progressDialog();
Dispatcher pdDispatcher = ((Window)this.progressDialog).Dispatcher;
this.worker = new BackgroundWorker();
this.worker.WorkerSupportsCancellation = true;
this.worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
this.worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
foreach (Element id in state.Elements)
{
if (this.worker.CancellationPending)
{
args.Cancel = true;
return;
}
UpdateProgressDelegate update = new UpdateProgressDelegate(this.UpdateProgressText);
pdDispatcher.BeginInvoke(update, Convert.ToInt32(((decimal)state.Elements.IndexOf(id)/(decimal)state.Elements.Count()) * 100), state.Elements.Count());
Element newElement = this.Item.Copy(id);
if (newElement .HasValue)
{
state.CreatedElements.Add(newElement.Value);
}
}
};
this.worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
if (!(args.Error == null))
{
throw args.Error;
}
this.UpdateDisplayItems();
this.progressDialog.Close();
this.progressDialog.Dispose();
};
this.worker.RunWorkerAsync();
this.progressDialog.Show();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressDialog.Value = e.ProgressPercentage;
}
public void UpdateProgressText(int percentage, int recordCount)
{
this.progressDialog.Description = string.Format("{0}% of {1} Records", percentage.ToString(), recordCount);
this.progressDialog.Value = percentage;
}
我看不到你在'DoWork'函数中的任何地方调用'worker.ReportProgress'? – Dennis 2012-02-23 06:36:09
@ Dennis我正在使用UpdateProgressText委托调用百分比到我的progressdialog – biju 2012-02-23 06:46:04
progressDialog是自定义对话框吗?你是否将progressDialog.Value绑定到ProgressBar控件?如果您可以分享progressDialog的代码,那将会很好。 – 2012-02-23 07:13:07