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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sunbags
    New Member
    • Oct 2007
    • 5

    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.
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    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.

    Comment

    • code937
      New Member
      • Sep 2007
      • 24

      #3
      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 :)

      Comment

      • code937
        New Member
        • Sep 2007
        • 24

        #4
        lol, same idea, posted the same time :)

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Originally posted by code937
          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)

          Comment

          • Sunbags
            New Member
            • Oct 2007
            • 5

            #6
            Originally posted by hariharanmca
            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.

            Comment

            • code937
              New Member
              • Sep 2007
              • 24

              #7
              Originally posted by Sunbags
              = What is a flag and how can I use it?
              right at the top of your code out of any routiene put this:

              Code:
              dim userxit as Boolean
              in your module code put this

              Code:
              if userexit = true then exit sub
              then on the command button put this

              Code:
              userexit = true
              That will do it :)

              Comment

              • Sunbags
                New Member
                • Oct 2007
                • 5

                #8
                Originally posted by code937
                right at the top of your code out of any routiene put this:

                Code:
                dim userxit as Boolean
                in your module code put this

                Code:
                if userexit = true then exit sub
                then on the command button put this

                Code:
                userexit = true
                That will do it :)
                Cheers, will try that now!

                Comment

                • hariharanmca
                  Top Contributor
                  • Dec 2006
                  • 1977

                  #9
                  [CODE=vb]
                  Do While x < 50
                  For x = 0 To 7
                  DoEvents
                  Next x
                  For x = 6 To 0 Step -1
                  Do Events
                  Next x
                  Loop
                  End Sub[/CODE]

                  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?

                  Comment

                  • Sunbags
                    New Member
                    • Oct 2007
                    • 5

                    #10
                    Originally posted by code937
                    right at the top of your code out of any routiene put this:

                    Code:
                    dim userxit as Boolean
                    in your module code put this

                    Code:
                    if userexit = true then exit sub
                    then on the command button put this

                    Code:
                    userexit = true
                    That will do it :)
                    Hello again, I implemented your code and the program still crashes. Also, when you said to place the
                    Code:
                    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
                    Code:
                    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?

                    Comment

                    • Sunbags
                      New Member
                      • Oct 2007
                      • 5

                      #11
                      Originally posted by hariharanmca
                      [CODE=vb]
                      Do While x < 50
                      For x = 0 To 7
                      DoEvents
                      Next x
                      For x = 6 To 0 Step -1
                      Do Events
                      Next x
                      Loop
                      End Sub[/CODE]

                      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!

                      Comment

                      • code937
                        New Member
                        • Sep 2007
                        • 24

                        #12
                        this refers toa routiene you have called Member, find it change the name and change where you call it from.

                        The Dimming is correct :)


                        Originally posted by Sunbags
                        Hello again, I implemented your code and the program still crashes. Also, when you said to place the
                        Code:
                        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
                        Code:
                        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?

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by code937
                          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.
                          Last edited by Killer42; Oct 6 '07, 03:38 AM.

                          Comment

                          • FairTaxBabe
                            New Member
                            • Sep 2008
                            • 1

                            #14
                            This post was very helpful to me. Having to do on the job training.
                            Thank you!

                            Comment

                            Working...