I want to add a progress bar to my Winforms project, but I have a problem.
In this case it won't close after 2 seconds
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.ShowDialog();
System.Threading.Thread.Sleep(2000);
form1.Close();
}
In this case the progress bar will not move
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
System.Threading.Thread.Sleep(2000);
form1.Close();
}
May I ask how to solve this problem? Or is there a better progress bar plug-in in Visual Studio?
jmcilhinney has already explained it very clearly, you need to understand the threading issues well.
Here is a method that does not need to use a timer, please refer to the following code:
Form2 form2 = new Form2();
Task.Factory.StartNew(() =>
{
form2.ShowDialog();
});
this.Invoke(new Action(() =>
{
if (form2 != null)
{
System.Threading.Thread.Sleep(2000);
form2.Close();
form2.Dispose();
form2 = null;
}
}));