stuck in loop. Please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steve marchant

    stuck in loop. Please help

    trying to learn VB6. Simple counting loop which counts to 8 in 1 sec
    intervals, then starts from 1 again and repeats.
    Have two Command buttons on the form. Cmd1 starts the counting, and I need
    to know how to stop it with Cmd2. Here's my code so far:
    Private Sub Command1_Click( )
    Dim x, y, m
    m = 1

    Do
    Print m
    x = Timer
    Do Until Timer = x + 1
    y = Timer
    Loop

    m = m + 1

    If m > 8 Then
    m = 1
    End If

    Loop
    End Sub




  • Dikkie Dik

    #2
    Re: stuck in loop. Please help

    Putting a "DoEvents" in the loop should give VB the chance to listen to
    the events of the second button.

    steve marchant wrote:[color=blue]
    > trying to learn VB6. Simple counting loop which counts to 8 in 1 sec
    > intervals, then starts from 1 again and repeats.
    > Have two Command buttons on the form. Cmd1 starts the counting, and I need
    > to know how to stop it with Cmd2. Here's my code so far:
    > Private Sub Command1_Click( )
    > Dim x, y, m
    > m = 1
    >
    > Do
    > Print m
    > x = Timer
    > Do Until Timer = x + 1
    > y = Timer
    > Loop
    >
    > m = m + 1
    >
    > If m > 8 Then
    > m = 1
    > End If
    >
    > Loop
    > End Sub
    >
    >
    >
    >[/color]

    Comment

    • steve marchant

      #3
      Re: stuck in loop. Please help

      see what you mean. Thanks
      "Dikkie Dik" <nospam@nospam. org> wrote in message
      news:b1177$448b 4d4e$57d40752$2 9309@news.versa tel.net...[color=blue]
      > Putting a "DoEvents" in the loop should give VB the chance to listen to
      > the events of the second button.
      >
      > steve marchant wrote:[color=green]
      >> trying to learn VB6. Simple counting loop which counts to 8 in 1 sec
      >> intervals, then starts from 1 again and repeats.
      >> Have two Command buttons on the form. Cmd1 starts the counting, and I
      >> need to know how to stop it with Cmd2. Here's my code so far:
      >> Private Sub Command1_Click( )
      >> Dim x, y, m
      >> m = 1
      >>
      >> Do
      >> Print m
      >> x = Timer
      >> Do Until Timer = x + 1
      >> y = Timer
      >> Loop
      >>
      >> m = m + 1
      >>
      >> If m > 8 Then
      >> m = 1
      >> End If
      >>
      >> Loop
      >> End Sub
      >>
      >>
      >>[/color][/color]

      Comment

      • Steve Gerrard

        #4
        Re: stuck in loop. Please help


        "steve marchant" <steve.c.marcha nt@tiscali.co.u k> wrote in message
        news:448b421d$1 _4@mk-nntp-2.news.uk.tisca li.com...[color=blue]
        > trying to learn VB6. Simple counting loop which counts to 8 in 1 sec
        > intervals, then starts from 1 again and repeats.
        > Have two Command buttons on the form. Cmd1 starts the counting, and I need to
        > know how to stop it with Cmd2.[/color]

        If you use a Do Loop like that, then you need to use DoEvents, as Dikkie Dik
        said.
        So here is an example: Lesson 1, Control Your Loops.

        You need a variable to control the loop, which Cmd1 sets one way, and Cmd2 sets
        the other way.
        It must be declared in the general section, not inside a procedure.
        You need to check that variable in both do loops.
        You need to use DoEvents in the loop, so the click of Cmd2 can do its work.
        It is also safer to check for Timer() > x + 1, not Timer() = x + 1.

        Something like this:

        Private StopGotClicked As Boolean

        Private Sub Command1_Click( )
        Dim x As Single
        Dim m As Integer

        StopGotClicked = False
        m = 1
        Cls

        Do Until StopGotClicked
        Print m
        x = Timer()
        Do Until StopGotClicked Or Timer() > x + 1
        DoEvents
        Loop
        m = m + 1
        If m > 8 Then m = 1
        Loop

        Print "stopped"

        End Sub

        Private Sub Command2_Click( )
        StopGotClicked = True
        End Sub


        Comment

        • Steve Gerrard

          #5
          Re: stuck in loop. Please help


          "steve marchant" <steve.c.marcha nt@tiscali.co.u k> wrote in message
          news:448b421d$1 _4@mk-nntp-2.news.uk.tisca li.com...[color=blue]
          > trying to learn VB6. Simple counting loop which counts to 8 in 1 sec
          > intervals, then starts from 1 again and repeats.
          > Have two Command buttons on the form. Cmd1 starts the counting, and I need to
          > know how to stop it with Cmd2.[/color]

          Having posted Lesson 1: Control Your Loops, now here is
          Lesson 2: Use a Timer Control Instead

          To see why, you will need to bring up Task Manager, so you can monitor CPU
          usage.
          The first method, using DoEvents, will cause your program to hog the CPU, at
          100% use.
          For counting off seconds (an eternity to a computer), this is quite a waste.

          Here is what the same program would look like using a timer control.
          I have named the timer control TimerCtrl to help avoid confusion.
          Note though that VB calls the timer tick event TimerCtrl_Timer ().
          Also note that the variables x and m have been moved out of procedures.

          The big advantage here is that your program checks the time every 1/10 of a
          second (interval = 100 msec), and then releases the CPU until the next timer
          event fires. The CPU use will stay around 1% max. DoEvents is not needed,
          because you are not hogging the CPU in the first place.

          Private x As Single
          Private m As Single

          Private Sub Form_Load()
          TimerCtrl.Inter val = 100
          TimerCtrl.Enabl ed = False
          End Sub

          Private Sub Command1_Click( )
          x = Timer()
          m = 1
          Cls
          Print m
          TimerCtrl.Enabl ed = True
          End Sub

          Private Sub Command2_Click( )
          TimerCtrl.Enabl ed = False
          Print "stopped"
          End Sub

          Private Sub TimerCtrl_Timer ()
          If Timer() > x + 1 Then
          m = m + 1
          If m > 8 Then m = 1
          Print m
          x = Timer()
          End If
          End Sub


          Comment

          • steve marchant

            #6
            Re: stuck in loop. Please help


            "Steve Gerrard" <mynamehere@com cast.net> wrote in message
            news:JpednTO7e4 kZxxbZnZ2dnUVZ_ oadnZ2d@comcast .com...[color=blue]
            >
            > Having posted Lesson 1: Control Your Loops, now here is
            > Lesson 2: Use a Timer Control Instead
            >[/color]
            snip

            Thanks,Steve, for both lessons. Quantum step in my VB learning curve.


            Comment

            • Angelo

              #7
              Re: stuck in loop. Please help

              I think the best way to do it is to use a Timer wich does every ...
              milliseconds 1 to the variable and shows it. do not use a do loop then. You
              can enable it, (starting the count) by using Timer1.Enabled = True, and
              disable it, (stopping the counter) by using Timer1.Enabled = False.

              Hope this is good information.

              Grtz.
              [color=blue]
              > trying to learn VB6. Simple counting loop which counts to 8 in 1 sec
              > intervals, then starts from 1 again and repeats.
              > Have two Command buttons on the form. Cmd1 starts the counting, and I need
              > to know how to stop it with Cmd2. Here's my code so far:
              > Private Sub Command1_Click( )
              > Dim x, y, m
              > m = 1
              >
              > Do
              > Print m
              > x = Timer
              > Do Until Timer = x + 1
              > y = Timer
              > Loop
              >
              > m = m + 1
              >
              > If m > 8 Then
              > m = 1
              > End If
              >
              > Loop
              > End Sub
              >
              >
              >
              >[/color]


              Comment

              • steve marchant

                #8
                Re: stuck in loop. Please help


                "Angelo" <angelo2@lancop .invalid> wrote in message
                news:44919581$0 $28375$e4fe514c @dreader11.news .xs4all.nl...[color=blue]
                >I think the best way to do it is to use a Timer wich does every ...
                >milliseconds 1 to the variable and shows it. do not use a do loop then. You
                >can enable it, (starting the count) by using Timer1.Enabled = True, and
                >disable it, (stopping the counter) by using Timer1.Enabled = False.
                >
                > Hope this is good information.
                >
                > Grtz.[/color]
                Thank you,Angelo. Steve Gerrard, earlier in this thread, pointed out that
                the Timer Event method was more efficient, and the code example he presented
                there worked well.


                Comment

                Working...