473,493 Members | 4,347 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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


Jun 10 '06 #1
7 7104
Putting a "DoEvents" in the loop should give VB the chance to listen to
the events of the second button.

steve marchant wrote:
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

Jun 10 '06 #2
see what you mean. Thanks
"Dikkie Dik" <no****@nospam.org> wrote in message
news:b1***************************@news.versatel.n et...
Putting a "DoEvents" in the loop should give VB the chance to listen to
the events of the second button.

steve marchant wrote:
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


Jun 10 '06 #3

"steve marchant" <st**************@tiscali.co.uk> wrote in message
news:44**********@mk-nntp-2.news.uk.tiscali.com...
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.


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
Jun 10 '06 #4

"steve marchant" <st**************@tiscali.co.uk> wrote in message
news:44**********@mk-nntp-2.news.uk.tiscali.com...
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.


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
Jun 10 '06 #5

"Steve Gerrard" <my********@comcast.net> wrote in message
news:Jp******************************@comcast.com. ..

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

snip

Thanks,Steve, for both lessons. Quantum step in my VB learning curve.
Jun 11 '06 #6
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.
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

Jun 15 '06 #7

"Angelo" <an*****@lancop.invalid> wrote in message
news:44***********************@dreader11.news.xs4a ll.nl...
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.

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.
Jun 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4228
by: ron | last post by:
have been stuck on this for several days now. I am trying to create a reverse polish calculator and I'm stuck at an intermediate stage. This is what I know I have to do (just not sure how to do it...
12
4870
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
17
2006
by: JT | last post by:
Help me the following C++ question: Write a program to help a local bookshop automate its billing system. The program should do the following: (a)Let the user enter the ISBN, the system will...
8
2934
by: MLH | last post by:
I use a mouse-down procedure to trap right mouse clicks and CTRL-Right mouse clicks. Running the procedure must put honey or some other sticky substance into my keyboard because subsequent...
5
2769
by: Mike D | last post by:
Attached is my code. Which I know there is a better way of writing however this is what I came up with and it works until the value is null or not = to the <> value. 'Do While rdrSQL.Read() ...
4
3111
by: =?Utf-8?B?TWF1cg==?= | last post by:
My cd is stuck in the drive. I can open the drawer O K but the c d will not come out Help me please -- Maur
1
1683
by: =?Utf-8?B?VHJ1cHRpIERhbGlh?= | last post by:
Hi to all, I am new in this newsgroup and new to .NET also. I am creating an application where I have to perform Mail Merge from my program. I have created a form which contain RTB and user can...
12
3201
by: beatjunkie27 | last post by:
I am working on a class assignment called Pennies for Pay the object of the program is to receive input for number of days worked. This should be > 0 and <= 40. An example output is below and...
2
1667
by: hexusnexus | last post by:
I wrote a simple algorithm and it keeps getting stuck in a loop. I guess I'm just to tired to figure it out: compcount= suitrank= trump=2 l,lt=0,0 while l<4: while lt<4:
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.