Link to home
Start Free TrialLog in
Avatar of bpl5000
bpl5000

asked on

Stopping code execution with a stop button on a VB6 form

I will be using the following code in a VB6 program to cause the CPU to kick up.  I will have a start and stop button on the form, but I'm not sure how to have the stop button cancel the execution of the code?  Here is the code that will be in the start button sub:

Dim goal
Dim before
Dim x
Dim y
Dim i
 
goal = 2181818
 
 
Do While True
      before = Timer
      For i = 0 to goal
            x = 0.000001
            y = sin(x)
            y = y + 0.00001
      Next
      y = y + 0.01
Loop

How would I make the stop button work?
Thanks! BPL
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
in your Do While Loop you can add DoEvents as well, so:

...

Do While True

if isExist then exit do
DoEvents

      before = Timer
      For i = 0 to goal
            x = 0.000001
            y = sin(x)
            y = y + 0.00001
      Next
      y = y + 0.01
Loop

...
Yeah...you definitely need that DoEvents.  Otherwise the Stop button press won't get processed until the While loop drops out.
Avatar of bpl5000
bpl5000

ASKER

That works great!  Thanks for the help!