title: How to break a loop with a single button click
body: <p>I am using VB.NET and I have:</p>
  
  <ul>
  <li>a <code>Start</code> button</li>
  <li>a <code>Stop</code> button</li>
  <li>a <code>While</code> loop</li>
  </ul>
  
  <p>When the <code>Start</code> button is pressed, the <code>While</code> loop begins.  I want to stop the loop when the <code>Stop</code> button is pressed.</p>
  
  <p>I had tried to use <code>Applications.DoEvents</code>, but the loop stops when the <code>Stop</code> button is pressed <strong>twice</strong>.</p>
  
  <p>Below is my code using <code>Applications.DoEvents</code></p>
  
  <pre><code>Dim stopclick As Boolean = False
  
  Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
      btnForward.Enabled = False
      btnStop.Enabled = True
      While 
          ' Perform the while statements
          If stopclick = True Then
              stopclick = False
              Application.DoEvents()
              Exit While
          End If
      End While
  End Sub
  
  Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
      stopClick = True
  End Sub
  </code></pre>
  
  <ol>
  <li>Have I used <code>Application.DoEvents</code> incorrectly?</li>
  <li>What are the other options besides <code>Application.DoEvents</code>?  How can I stop the loop with a single <code>Stop</code> button click?</li>
  </ol>