Results 1 to 11 of 11

Thread: [RESOLVED] Pause in a loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    2

    Resolved [RESOLVED] Pause in a loop

    I am extreemly new at this, but my tutors couldnt help either

    I am using VB6

    I am using a for-next and want to pause inbetween each go but cant work out how

    code Code:
    1. for a=10 to 300 step 10
    2. line(a,100)-(a-400)
    3. next a

    I want a pause inbetween drawing each line
    I have tried sleep or putting a for-next loop to slow it down but I cant get it to work properly

    Any help would be nice

    Thanks

    Ady

  2. #2
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Pause in a loop

    The better way to accomplish this, then, would probably be a Timer control. That way you are running the code at a set interval, instead of looping but then having to slow it down.

  3. #3
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Pause in a loop

    you could use a timer
    Code:
    '=======in decalartion area
    Dim Frequency As Long
    '=======form load
    Private Sub Form_Load()
    Timer1.Interval = 60000
    Frequency = 1
    End Sub
    
    '========timer event
    Private Sub Timer1_Timer()
    If Frequency = 15 Then
        'do your staff
        Frequency = 0
    end if
    Frequency = Frequency + 1
    End Sub
    Change the interval and frequency to what you want
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    Member
    Join Date
    Nov 2008
    Posts
    55

    Re: Pause in a loop

    vb Code:
    1. For a = 10 To 300 Step 10
    2.     Line (a, 100)-(a, 400)
    3.     For I = 0 To 50000
    4.         DoEvents
    5.     Next I
    6. Next a

  5. #5
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Pause in a loop

    tropix.... that is not a good thing to do... at all. DoEvents is a command to make sure windows can process things so things won't freeze up... Calling that fifty thousand times in a row will pretty much defeat the purpose. He said he wanted a pause in between line draws and the appropriate answer has been given... twice in fact.

  6. #6
    Member
    Join Date
    Nov 2008
    Posts
    55

    Re: Pause in a loop

    danecook..... I totally agree with what you have said here. the purpose of the doevents is to enable the same escape from the loop if needed. This would not be a normal call to use in a more complex program which was performing other functions and or sub routines. In such a small proceure, it is quite acceptable.
    As an experienced programmer as yourself you will be well aware that there is never a right or wrong way to get there....only a more or less efficient.

  7. #7
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Pause in a loop

    I don't know if I agree with that last statement but if so, that way is so inefficient it could be called wrong I mean it is literally just wasting away CPU time. If the task you're doing requires that, you need to rethink your logic.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Pause in a loop

    Quote Originally Posted by danecook21
    I don't know if I agree with that last statement but if so, that way is so inefficient it could be called wrong I mean it is literally just wasting away CPU time. If the task you're doing requires that, you need to rethink your logic.
    I'm not sure, but the timer control internally just keeps looping same as the code above. How else could it know that it's time for next iteration to begin?

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Pause in a loop

    The timer control does not loop like that - if you compile 2 programs (one with a loop, and one with a timer), you will see that the one with the timer uses much less CPU time than the loop equivalent.

    I don't really know what methods the timer uses, but suspect it is using some kind of API timer (which is fired by Windows at the apt time, as part of its other work).

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    2

    Re: Pause in a loop

    ok thanks all for your answers.

    Only thing is when I said I am new I mean really new. The only reason VB was introduced to our lesson was to illistrate algebra.
    The only commands we were shown was line, for next and step.

    I have been playing (basically trying to use my old zx spectrum basic)
    and have now made lots of pretty paterns.

    Now I am trying to make my patterns appear gradually rather than just popping up finished.
    So what I am really asking is could someone please just show me the code from top to bottom for the simple loop I put at the begining so I could just copy and paste it onto form1 and it run. That way I could understand it better then I could adapt it to fit into my pattern.

    Thanks in advance

    Ady

  11. #11
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Pause in a loop

    Here's some code that you can copy & paste. One procedure uses a For Next loop with a Pause statement in it. The Pause statement calls the Pause Sub on each loop. When the loop is exited it fires the timer that repeats the action without a loop. This time the timer code moves the Line. Basically, these two procedures demonstrate two ways of doing the same thing.
    Put a Timer, CommandButton and two Line Controls on your form. Then copy and paste this code. Note how the Pause Sub is used here. This is an extremely versatile sub and is definitely one of my favorites.
    Code:
    Option Explicit
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Sub Form_Load()
       Me.WindowState = vbMaximized
       Timer1.Enabled = False
    End Sub
    
    Private Sub Command1_Click()
       Static i As Integer
       For i = 1 To 5000 Step 10
         ' DoEvents
          i = i + 1
             Line1.X2 = i + 1000
             Line1.X1 = i + 1000
          Pause 0.01                          ' Pause 10 milliseconds
          Show
       Next
       Timer1.Enabled = True
       Timer1.Interval = 10                ' Timer Interval is 10 milliseconds.
    End Sub
    
    ' Credits: (Milk (Sleep+Pause Sub)). (Wayne Spangler (Pause Sub))
    Private Sub Pause(ByVal Delay As Single)
       Delay = Timer + Delay
       If Delay > 86400 Then 'more than number of seconds in a day
          Delay = Delay - 86400
          Do
              DoEvents ' to process events.
              Sleep 1 ' to not eat cpu
          Loop Until Timer < 1
       End If
       Do
           DoEvents ' to process events.
           Sleep 1 ' to not eat cpu
       Loop While Delay > Timer
    End Sub
    
    Private Sub Timer1_Timer()
       Static i As Integer
         'DoEvents
          i = i + 10
          Line2.X2 = i + 1000
          Line2.X1 = i + 1000
          Pause 0.01
          Show
          If i = 5000 Then
             Timer1.Enabled = False
          End If
    End Sub
    Last edited by CDRIVE; Nov 13th, 2009 at 06:14 PM.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width