• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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
0
bpl5000
Asked:
bpl5000
  • 2
1 Solution
 
Ryan ChongCommented:
try like:

At top of your moduel, add:

Dim isExist As Boolean


Dim goal
Dim before
Dim x
Dim y
Dim i
 
goal = 2181818
 

isExist = False

Do While True

if isExist then exit do

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


then in your Stop button, add:

isExist = True


Get the idea?
0
 
Ryan ChongCommented:
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

...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yeah...you definitely need that DoEvents.  Otherwise the Stop button press won't get processed until the While loop drops out.
0
 
bpl5000Author Commented:
That works great!  Thanks for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now