I have a mainform with a simple button to test my loading screen.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void loadingscreenTest(object sender, EventArgs e)
{
int count = 100;
LoadingScreen f2 = new LoadingScreen(0, count, 1);
f2.Show();
//Application.DoEvents();
for (int i = 0; i < count; i++)
{
System.Threading.Thread.Sleep(1000);
f2.performstep();
}
}
}
LoadingScreen is a simple form with a ProgressBar on it.
It's code is the following:
public partial class LoadingScreen : Form
{
public LoadingScreen(int current, int max, int step)
{
InitializeComponent();
this.progressBar1.Maximum = max;
this.progressBar1.Step = step;
this.progressBar1.Value = current;
}
public void performstep()
{
this.progressBar1.PerformStep();
}
}
I know that you are not supposed to run long running processes in the UI thread, however in this scenario i can't see why is it that when i press the button, the LoadingScreen form has trouble drawing its content without the Application.DoEvents call. Can you please help me understand this phenomenon?
The Issue persists for like 1 to 2 seconds but what is buggin me is that I don't understand why.
I am using Visual Studio 2010 and .Net 4.0