Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
18495
Update UltraProgressBar while executing long-running process
posted

Not too long ago I had a case with a customer in which he wanted to know how he could update the UltraProgressBar during a long-running process.  I've decided to post the solution here so the rest of the community can benefit.

The best way to go about doing this is to use a BackgroundWorker to execute your long-running process.  I've attached a sample project which demonstrates this.  First, you need to create a method in which you'll update the progress bar and a delegate for that method:

private delegate void updateProgressDelegate(int percent);
private void updateProgress(int percent)
{
	ultraProgressBar1.Value = percent;
}

Next, create the method that will execute your long-running process:

private void someLongRunningMethod()
{
	int totalIterations = 5000;
	for (int i = 0; i < totalIterations; i++)
	{
		int percent = (int)((float)i / (float)totalIterations * 100);
		Debug.WriteLine(i + ": " + percent + "%");
		this.Invoke(new updateProgressDelegate(updateProgress), percent);
		Thread.Sleep(5);
	}
}

Now, handle a couple events of the BackgroundWorker to take care of starting the process and cleaning up after the process has finished:

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
	someLongRunningMethod();
}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
	ultraProgressBar1.Value = 0;
	ultraButton1.Enabled = true;
}

Finally, create the code to actually start the process:

private void ultraButton1_Click(object sender, EventArgs e)
{
	worker.RunWorkerAsync();
	ultraButton1.Enabled = false;
}
BackgroundWorkerProgress.zip
Parents
No Data
Reply Children
No Data