Progress Bar For Long Running Task In C#
My application runs some database queries that can take a long time. While executing these queries, my application appears to freeze and it looks like the application has stopeed w
Solution 1:
You may use BackgroundWorker() to solve this kind of problems.
First of all define a global variable of class BackgroundWorker()
like
private BackgroundWorker bgw;
then use below code in starting of your query execution like button1_Click() event or anything.
bgw = newBackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.ProgressChanged += newProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += newRunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.DoWork += newDoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerAsync();
Now define the methods as below:
voidbgw_DoWork(object sender, DoWorkEventArgs e)
{
//Your time taking work. Here it's your data query method.CheckSsMissingDate();
}
voidbgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Progress bar.
progressBar1.Value = e.ProgressPercentage;
}
voidbgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//After completing the job.MessageBox.Show(@"Finished");
}
Solution 2:
If you don't want your application to freeze,you should use the async/await keywords recently implemented in C#,or you could write your async code manually.
Reference : MSDN
Post a Comment for "Progress Bar For Long Running Task In C#"