C#: Wait until progressbar finished drawing

janw picture janw · Aug 23, 2011 · Viewed 7.8k times · Source

Possible Duplicate:
Winforms Progress bar Does Not Update (C#)

First time asking a question here for me.

I'll try to explain my problem using this code snippet:

progressBar1.Maximum = 50;
for (int i = 0; i < 50; i++)
{
    progressBar1.Value++;
}
MessageBox.Show("Finished");
progressBar1.Value = 0;

The problem with this code is that the MessageBox pops up at the time the for loop is finished, not when the progressbar has finished drawing. Is there any way to wait until the progressbar has finished drawing before continuing?

Thanks guys!

Answer

fjdumont picture fjdumont · Aug 23, 2011

You might want to have a look at System.Windows.Forms.Application.DoEvents(). Reference

progressBar1.Maximum = 50;
for (int i = 0; i < 50; i++)
{
    progressBar1.Value++;
    Application.DoEvents();
}
MessageBox.Show("Finished");
progressBar1.Value = 0;