I’m new to C# and have created a basic program that I’m running through Visual Studio 2010. When the window is closed, the program stops. However there’s a delay of a few seconds before the IDE goes back to edit mode. How can I immediately end the program when the window is closed?
private void button1_Click(object sender, EventArgs e){
While (Visible) {
for (int c = 0; c < 254) {
this.BackColor = Color.FromArgb(c, 255 - c, c);
Application.DoEvents();
System.Threading.Thread.Sleep(3);
}
for (int c = 254; c >= 0) {
this.BackColor = Color.FromArgb(c, 255 - c, c);
Application.DoEvents();
System.Threading.Thread.Sleep(3);
}
}
}
I’ve tried removing “System.Threading.Thread.Sleep(3);” from the loops but that still did not help fix the issue. I’ve also been able to reproduce this on multiple machines. Any ideas why this could be happening?
The delay happens because the for loops need to finish before the while loop can check if Visible is still true. I was able to fix it by adding && Visible == true to the conditional test in each for loop. That way the loop ends as soon as Visible turns false.