Reputation: 123
I'm using VS 2005 with C#. I have a query that takes about 25 to 30 seconds and show the result with crystal report.
I want to show progress bar while query executing.
My situation is same with the Progress bar for long running task in C#
I follow the steps in that post but the progress bar is blank.
Please see my code and advise what is needed.
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 !");
}
}
Upvotes: 2
Views: 4433
Reputation: 4528
Simply call ReportProgress
in the DoWork
method.
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);
}
This will means that you must change the getData1
method needs to report progress in chunks as it's busy.
If you don't want to change getData1
, set the IsInderminate
property to true on your ProgressBar
.
progressBar1.IsInderminate = true;
Then you don't need the bgw_ProgressChanged
method at all. DoWork
and RunWorkerCompleted
are sufficient.
Apologies, I assumed WPF. For.NET 2 you're using WinForms?
WinForms: In the designer, on the ProgressBar
, change Style
to Marquee
.
Upvotes: 4