?
Solved

Stopping code execution with a stop button on a VB6 form

Posted on 2007-07-24
4
Medium Priority
?
193 Views
Last Modified: 2012-05-05
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
Comment
Question by:bpl5000
  • 2
4 Comments
 
LVL 54

Accepted Solution

by:
Ryan Chong earned 1000 total points
ID: 19557294
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
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 19557321
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19557331
Yeah...you definitely need that DoEvents.  Otherwise the Stop button press won't get processed until the While loop drops out.
0
 
LVL 5

Author Comment

by:bpl5000
ID: 19557510
That works great!  Thanks for the help!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question