473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Breaking a "Do While" Infinite Loop using a command button

Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the knight-rider light sequence. Obviously, this means using a loop and our lecturer has told us to use timers to control how long the sequence lasts for. I don't want to use timers ((this is not due to laziness, I would have been finished long ago had I used timers!! I just want to see if there is another way to do this project) , and I've read everywhere that the only way to break out of an infinite loop is Ctl+Alt+Del! What I am asking is - is there any code I could use to over-write the loop which I could also link with a command button (i.e. when I hit my 'exit' button on my form, the program stops runnning). If this is not possible, a way in which the user could input a number which would stop the program would also be a great help. The module I'm studying this for is interfacing, so user-compatability is paramount. Perhaps there is a different loop I could use which offers more flexibility?

Here is an outline of my code so far:

Private Sub cmdstart_click()
Dim Index As Integer

Do While x < 50 (There's no particular reason why I chose 50, I just wanted to create an infinite loop)

For x = 0 To 7

DoEvents

Next x

For x = 6 To 0 Step -1

Do Events

Next x

Loop




End Sub

Code for 'Exit' Button

Private Sub Command1_Click()
Unload Me
End Sub

I haven't posted the 'events' in the code, as the posting guidlines advise that you shouldn't post all of your code. I need to have this done in two days, so any help is appreciated! Thanks in advance, Anna.
Oct 3 '07 #1
13 17908
hariharanmca
1,977 1GB
Your code doesn’t make any sense. Because X will not go beyond 50.
use boolen Flag, timer control if the flag is false then run loop if it true then stop.
Oct 3 '07 #2
code937
24
Surely you'd just set a flag?

at the top of your project set userexit as boolian

then in your loop check if userexit = true is so then exit the loop

then on the command button (user input) just set userexit = true.


in 'theory' that would work, try it :)
Oct 3 '07 #3
code937
24
lol, same idea, posted the same time :)
Oct 3 '07 #4
hariharanmca
1,977 1GB
lol, same idea, posted the same time :)
Cheer up code937.

(The message you have entered is too short. Please lengthen your message to at least 20 characters)
Oct 3 '07 #5
Your code doesn’t make any sense. Because X will not go beyond 50.
use boolen Flag, timer control if the flag is false then run loop if it true then stop.
That was the idea, that it never goes beyond x, simply because I wanted to loop to run until the user decides to exit. However, exiting a loop is not so easy! What is a flag and how can I use it? I only started learning VB6 a week or two ago, so I'm not familiar with every nook and cranny, as it were! Thanks for your reply.
Oct 3 '07 #6
code937
24
= What is a flag and how can I use it?
right at the top of your code out of any routiene put this:

Expand|Select|Wrap|Line Numbers
  1. dim userxit as Boolean
in your module code put this

Expand|Select|Wrap|Line Numbers
  1. if userexit = true then exit sub
then on the command button put this

Expand|Select|Wrap|Line Numbers
  1. userexit = true
That will do it :)
Oct 3 '07 #7
right at the top of your code out of any routiene put this:

Expand|Select|Wrap|Line Numbers
  1. dim userxit as Boolean
in your module code put this

Expand|Select|Wrap|Line Numbers
  1. if userexit = true then exit sub
then on the command button put this

Expand|Select|Wrap|Line Numbers
  1. userexit = true
That will do it :)
Cheers, will try that now!
Oct 3 '07 #8
hariharanmca
1,977 1GB
Expand|Select|Wrap|Line Numbers
  1. Do While x < 50 
  2. For x = 0 To 7
  3. DoEvents
  4. Next x
  5. For x = 6 To 0 Step -1
  6. Do Events
  7. Next x
  8. Loop
  9. End Sub
First 'for' will not do any thing (only Do events)
and also Same to second 'for'.
for this why don't you use a timer control?
Oct 3 '07 #9
right at the top of your code out of any routiene put this:

Expand|Select|Wrap|Line Numbers
  1. dim userxit as Boolean
in your module code put this

Expand|Select|Wrap|Line Numbers
  1. if userexit = true then exit sub
then on the command button put this

Expand|Select|Wrap|Line Numbers
  1. userexit = true
That will do it :)
Hello again, I implemented your code and the program still crashes. Also, when you said to place the
Expand|Select|Wrap|Line Numbers
  1. dim userexit as Boolean
at the top of the code outside of all routines, I presumed you meant that I was to put it above the
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdStart_Click()
line as opposed to underneath it. However, when I try to compile this, I get a message saying "Compile Error: Member already exists in an object module from which this object module derives". Am I placing the code you've given me in the wrong place?
Oct 4 '07 #10
Expand|Select|Wrap|Line Numbers
  1. Do While x < 50 
  2. For x = 0 To 7
  3. DoEvents
  4. Next x
  5. For x = 6 To 0 Step -1
  6. Do Events
  7. Next x
  8. Loop
  9. End Sub
First 'for' will not do any thing (only Do events)
and also Same to second 'for'.
for this why don't you use a timer control?
I realise that 'for' can't do events, the 'DoEvents' was a phrase I used merely to represent a block of code, as the posting guidelines state that you shouldn't post all of your code. Sorry about that, it must have looked very strange!! Anyway, my lecturer did tell me to use timer controls, but I'm interested in finding out if you can break an infinite loop because that would be a much better way of doing things, if at all possible. Here's hoping!
Oct 4 '07 #11
code937
24
this refers toa routiene you have called Member, find it change the name and change where you call it from.

The Dimming is correct :)


Hello again, I implemented your code and the program still crashes. Also, when you said to place the
Expand|Select|Wrap|Line Numbers
  1. dim userexit as Boolean
at the top of the code outside of all routines, I presumed you meant that I was to put it above the
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdStart_Click()
line as opposed to underneath it. However, when I try to compile this, I get a message saying "Compile Error: Member already exists in an object module from which this object module derives". Am I placing the code you've given me in the wrong place?
Oct 6 '07 #12
Killer42
8,435 Expert 8TB
this refers toa routiene you have called Member, find it change the name and change where you call it from.

The Dimming is correct :)
code937, I think you're sending the OP on a bum steer, here. It's highly unlikely that he/she has a routine called Member. I think he/she didn't understand where to place form-level declarations.

Sunbags, could you show us the code that you have? That "Member already exists" thing should be pretty simple to correct.

And to try and clarify things a bit...
  • You do need to have a DoEvents statement somewhere inside your infinite loop. This statement tells your program to take a break, and allow Windows to handle other important things, such as the user interface. Without that, you will not be able to click the button to set the flag, because Windows won't be able to respond to the click.
  • The problems with the "userexit" declaration relate to variable scope. You may or may not have covered this. If you declare (Dim) a variable inside a routine (Sub or Function) then it only exists within that routine. Which means that if you declare it in the routine that has the loop, nothing else outside oif that routine (such as the button click event) can touch it. Which makes it, of course, completely useless.
    What you need to do is put the Dim (or Private) statement at the top of the form code, before any of the Sub or Function declarations. In other words, at the top of the form's code you would have something like...
    Option Explicit
    Private userexit As Boolean
    .
    .
    .
    ' Then your Subs and Functions
  • If you just moved the Dim statement "above" the Sub, it was probably still after some other code. Which is probably what caused the compile error.
  • In case you're wondering, the Option Explicit is not actually required, but does prevent a lot of problems. In your options, you should have "Require explicit variable declaration" turned on. What that does is place this statement at the top of each module by default. This statement tells VB to insist that you create each variable before using it. Without this, any time you use a variable name that hasn't been declared, VB will just create one of the default type (probably Variant). Sounds nice, but it means any time you type a variable name wrong, VB won't tell you. It'll just create a new variable and keep going, and you can waste days trying to work out where your information went.
Oct 6 '07 #13
This post was very helpful to me. Having to do on the job training.
Thank you!
Sep 17 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been...
6
by: Lauren Wilson | last post by:
Hello group, Somehow, I have not yet fully understood the meaning and purpose of "DoEvents". Can someone post or point me to a clear, concise explanation of it -- one that includes situational...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
9
by: haijin.biz | last post by:
why the following program using "char" gives me an infinite loop? If I change char to int, it works fine. #include <iostream> using namespace std; int main(int argc, char** argv) { for(char...
3
by: archeryguru2000 | last post by:
Hello, I've been stumped for several weeks on this particlar program. At work here, we have 30 machines that we record scrap data on. I take this data and create spreadsheets (boring) that can be...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
8
by: Pivot_Tables | last post by:
Hi, I have created a recursive SQL Query in DB2 and it works fine until some point in the tree where the data gets into infinite loop. Below are some sample data from my relationship table. ...
1
by: robotlizz | last post by:
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.