title: How To Immediately End A Program When The Window Is Closed
body: <p>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?</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e){
      While (Visible) {
          for (int c = 0; c &lt; 254) {
              this.BackColor = Color.FromArgb(c, 255 - c, c);
              Application.DoEvents();
              System.Threading.Thread.Sleep(3);
          }
          for (int c = 254; c &gt;= 0) {
              this.BackColor = Color.FromArgb(c, 255 - c, c);
              Application.DoEvents();
              System.Threading.Thread.Sleep(3);
          }
      }
  }
  </code></pre>
  
  <p>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?</p>