我正在使用VS 2005和C#。我有一个大约需要25到30秒的查询,并用水晶报告显示结果
我想在执行查询时显示进度条
我的情况与Progress bar for long running task in C#相同
我按照该帖子中的步骤操作,但进度条为空白
请查看我的代码并告知我们需要什么。
private string orderby, startDate, endDate, brandName;
ReportDocument rpt;
private BackgroundWorker bgw;
private void SearchFunction()
{
orderby = cboOrderBy.Text;
startDate = txtFromDate.Text;
endDate = txtToDate.Text;
brandName = txtBrandName.Text;
bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerAsync();
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
dt = obj1.getData1(orderby, startDate, endDate, brandName);
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//After completing the job.
MessageBox.Show(@"Finished");
if (dt.Rows.Count > 0)
{
//some code for crystal report like
rpt = new myReport();
rpt.SetDataSource(dt);
crystalReportViewer1.ReportSource = rpt;
crystalReportViewer1.Refresh();
}
else
{
MessageBox.Show("No record found !");
}
}
答案 0 :(得分:4)
只需在ReportProgress
方法中调用DoWork
。
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
int percentComplete = 0;
// Do work and let the work increment percepercentComplete as it's busy.
bgw.ReportProgress(percentComplete);
}
这意味着您必须更改getData1
方法,以便在忙碌时报告块的进度。
如果您不想更改getData1
,请在IsInderminate
上将ProgressBar
属性设置为true。
progressBar1.IsInderminate = true;
然后你根本不需要bgw_ProgressChanged
方法。 <{1}}和DoWork
就足够了。
WinForms:在设计人员的RunWorkerCompleted
上,将ProgressBar
更改为Style
。
答案 1 :(得分:1)