I'm not sure if this is the best solution, but I have it like this:
//this is the action item (button click) private void importSFNFReportButton_Click(object sender, EventArgs e) { //I run backgroundWorker6Progress.RunWorkerAsync(); //this is how I start the progress bar 'movement' bgwImportSF.RunWorkerAsync(); //this is another task that is lauchned after the progress bar is initiated }
This is the actual background worker.
private void backgroundWorker6Progress_DoWork(object sender, DoWorkEventArgs e) { bool cont = true; while (cont) { PauseForMilliSeconds(100); updateProgressbar1(false); if (noTasksExistCheck()) { updateProgressbar1(true); cont = false; } } }
this is a delegate - I call it to automatically increase the progress bar indicator
delegate void updateProgressBarStatus(bool done); private void updateProgressbar1(bool done) { if (progressBar1.InvokeRequired) { updateProgressBarStatus del = new updateProgressBarStatus(updateProgressbar1); progressBar1.Invoke(del, new object[] { done }); } else { if (progressBar1.Value == progressBar1.Maximum) { progressBar1.Value = progressBar1.Minimum; } progressBar1.PerformStep(); if (done == true) { progressBar1.Value = progressBar1.Minimum; } } }
I control it through a function that should check the global variable
noTasksExistCheck()
This is a pause timer
public static DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor) { System.DateTime ThisMoment = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor); System.DateTime AfterWards = ThisMoment.Add(duration); while (AfterWards >= ThisMoment) { System.Windows.Forms.Application.DoEvents(); ThisMoment = System.DateTime.Now; } return System.DateTime.Now; }
source share