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 | | | | 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] | | | | re: stuck in loop. Please help
see what you mean. Thanks
"Dikkie Dik" <nospam@nospam.org> wrote in message
news:b1177$448b4d4e$57d40752$29309@news.versatel.n et...[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] | | | | re: stuck in loop. Please help
"steve marchant" <steve.c.marchant@tiscali.co.uk> wrote in message
news:448b421d$1_4@mk-nntp-2.news.uk.tiscali.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 | | | | re: stuck in loop. Please help
"steve marchant" <steve.c.marchant@tiscali.co.uk> wrote in message
news:448b421d$1_4@mk-nntp-2.news.uk.tiscali.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.Interval = 100
TimerCtrl.Enabled = False
End Sub
Private Sub Command1_Click()
x = Timer()
m = 1
Cls
Print m
TimerCtrl.Enabled = True
End Sub
Private Sub Command2_Click()
TimerCtrl.Enabled = 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 | | | | re: stuck in loop. Please help
"Steve Gerrard" <mynamehere@comcast.net> wrote in message
news:JpednTO7e4kZxxbZnZ2dnUVZ_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. | | | | 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] | | | | re: stuck in loop. Please help
"Angelo" <angelo2@lancop.invalid> wrote in message
news:44919581$0$28375$e4fe514c@dreader11.news.xs4a ll.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. |  | Similar Visual Basic 4 / 5 / 6 bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,358 network members.
|