I have a Winform application which does several queries to Excel-Datatables with oleDB. It works so far, but now I want to add a Progressbar which inform the User that the application is not hanging up but loading the necessary informations based on the user input.
Using Backgroundworker and a new task seems like that the progressbar and the whole UI freeze.
private void updateButton_Click(object sender, EventArgs e)
{
progressBar1.Visible = true;
progressBar1.Style = ProgressBarStyle.Marquee;
Task t1 = Task.Factory.StartNew(() => QueryToExcel());
Task.WaitAll(t1);
progressBar1.Visible = false;
}
private void QueryToExcel()
{
//doing some data manipulation with LINQ, oleDB etc.
//and finally stored in datatable FinalTable
datagridview1.datasource = FinalTable;
datagridview1.Update();
}
EDIT:
I believe the problem is, that Im setting the datagridview.datasource = Datatable while the progressbar is running, so everything is unresponsive. So what I want is, that the Backgroundworker is running the data manipulation and put the final datatable as datasource, while it is running, the progressbar should just continueing this marquee. If the backgroundworker has finished, then the progressbar should just disappear...
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
QueryToExcel();
}
private void updateButton_Click(object sender, EventArgs e)
{
progressBar1.Visible = true;
StartBackgroundWork();
}
private void StartBackgroundWork()
{
if (Application.RenderWithVisualStyles)
progressBar1.Style = ProgressBarStyle.Marquee;
else
{
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Maximum = 100;
progressBar1.Value = 0;
}
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Value = 0;
dataGridView1.Update();
progressBar1.Visible = false;
}