I need to use a "Run" button and a "Stop" button to control the execution of an internal subroutine. The following code works, but you have to click the Stop button twice to stop the routine. What do I have to do to get it to stop in a single click?
-----
' RunTest.exe
' Version 1.0.0
' MDJ 2012/06/27
' Label1 initially displays "STOPPED"
Option Strict On
Imports System.Windows
Public Class Form1
' Global Variable
Public runControl As Boolean
' Run Button
Private Sub RunButton_Click(sender As System.Object, e As System.EventArgs) Handles RunButton.Click
runControl = True
Label1.Text = "RUNNING"
While runControl = True
RunStep()
End While
End Sub
' Stop Button
Private Sub StopButton_Click(sender As System.Object, e As System.EventArgs) Handles StopButton.Click
' With this configuration, the Stop button must be clicked twice before the While Loop will exit ???
runControl = False
Label1.Text = "STOPPED"
End Sub
Public Sub RunStep()
Dim ValueHolder As Integer
' Process some data
For i = 1 To 79
For j = 1 To 79
ValueHolder = i * j + 23
Next
Next
' Allow outside events to be processed
Application.DoEvents()
End Sub
End Class