|
-
Nov 7th, 2008, 12:08 PM
#1
Thread Starter
New Member
[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:
for a=10 to 300 step 10
line(a,100)-(a-400)
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
-
Nov 7th, 2008, 12:19 PM
#2
Hyperactive Member
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.
-
Nov 7th, 2008, 12:30 PM
#3
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 
-
Nov 7th, 2008, 09:08 PM
#4
Member
Re: Pause in a loop
vb Code:
For a = 10 To 300 Step 10
Line (a, 100)-(a, 400)
For I = 0 To 50000
DoEvents
Next I
Next a
-
Nov 7th, 2008, 09:13 PM
#5
Hyperactive Member
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.
-
Nov 7th, 2008, 09:28 PM
#6
Member
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.
-
Nov 8th, 2008, 01:13 PM
#7
Hyperactive Member
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.
-
Nov 8th, 2008, 01:35 PM
#8
Re: Pause in a loop
 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
-
Nov 8th, 2008, 02:04 PM
#9
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).
-
Nov 9th, 2008, 12:08 PM
#10
Thread Starter
New Member
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
-
Nov 9th, 2008, 06:23 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|