is a loop the best way to create a delay

P

Paul

I use underlined labels in my forms with code in the On Click event so users
can execute commands by clicking on these labels. I do this to save space
and simplify the visual display, similar to the hyperlinks used to launch
commands in Internet Browsers.

In order to provide visual feedback that the button has been clicked, I've
added a function that momentarily changes the color of the label text, makes
it bold, and then restores the font appearance to normal after a 1 second
delay. I'm using a Do loop to create the delay, but I'm wondering if that's
the best way to do it.

The loop goes through 4 million iterations, and so I'm wondering if this is
the most efficient way to do this. Is this something the Jet engine can do
without breaking a sweat, or am I making it go through unnecessary paces
while there is a better way to create a delay?

Thanks in advance,

Paul
 
S

Stuart McCall

Paul said:
I use underlined labels in my forms with code in the On Click event so
users can execute commands by clicking on these labels. I do this to save
space and simplify the visual display, similar to the hyperlinks used to
launch commands in Internet Browsers.

In order to provide visual feedback that the button has been clicked, I've
added a function that momentarily changes the color of the label text,
makes it bold, and then restores the font appearance to normal after a 1
second delay. I'm using a Do loop to create the delay, but I'm wondering
if that's the best way to do it.

The loop goes through 4 million iterations, and so I'm wondering if this
is the most efficient way to do this. Is this something the Jet engine
can do without breaking a sweat, or am I making it go through unnecessary
paces while there is a better way to create a delay?

Thanks in advance,

Paul

Without doubt the best way to implement a delay is to use the API function
Sleep. This is the declaration:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

So for a delay of 1 second:

Sleep 1000

This way you'll use less CPU time and allow other processes to run normally.
 
P

Paul Shapiro

However you're still locking up the user interface for that 1 second. You
could also use the form timer, set it to trigger in 1 second, and have the
timer routine restore the color. That way the OnClick routine finishes
immediately and the user can take other action during that 1 second.
 
P

Paul

Interesting suggestion, Paul, and I'm sure it would work. However, if you
have a number of "active" labels on a given form as I do, you'd have to make
sure the Timer event restores whichever label was clicked. I guess you
could loop through all the labels on the form, but then you'd also have to
set the Timer event on all the forms in which you have such labels.

With the Sleep function, I can keep it in a Public Function in a General
Module, and call it directly from the click event of any control:

Private Sub Label6_Click()
Call purpleButton(Forms!frm_Main_Menu, Forms!frm_Main_Menu!Label6)
End Sub

Public Function purpleButton(formName As Object, buttonName As Object)
buttonName.ForeColor = 8388736
buttonName.FontBold = True
formName.Repaint
Sleep 500
buttonName.ForeColor = 10040115
buttonName.FontBold = False
End Function

If you want to change the function, you only have to do it in one place.
And you could easily add an additional parameter to control the color to be
flashed, or other characteristics of the visual feedback.

You're right that you're momentarily locking up the interfact (I've decided
that 1/2 second is long enough for feedback), but it's well worth it to let
the user know they've clicked the button, so they don't keep clicking it
repeatedly, while an operation is taking a few seconds to complete itself.

Best,

Paul
 
A

AG

Stuart,

How would that compare to a loop using doevents?
aircode
Dim sngTimer as Single
sngTimer = Timer + 1
Do
Doevents
Loop while Timer > sngTimer
 
S

Stuart McCall

AG said:
Stuart,

How would that compare to a loop using doevents?
aircode
Dim sngTimer as Single
sngTimer = Timer + 1
Do
Doevents
Loop while Timer > sngTimer

Give sngTimer a large value and run it with Task Manager open. Watch the CPU
usage leap.
 
M

Mike Painter

Paul said:
I use underlined labels in my forms with code in the On Click event
so users can execute commands by clicking on these labels. I do this
to save space and simplify the visual display, similar to the
hyperlinks used to launch commands in Internet Browsers.

In order to provide visual feedback that the button has been clicked,
I've added a function that momentarily changes the color of the label
text, makes it bold, and then restores the font appearance to normal
after a 1 second delay. I'm using a Do loop to create the delay, but
I'm wondering if that's the best way to do it.

The loop goes through 4 million iterations, and so I'm wondering if
this is the most efficient way to do this. Is this something the Jet
engine can do without breaking a sweat, or am I making it go through
unnecessary paces while there is a better way to create a delay?

Thanks in advance,

Another way to answer your question is for you to find and run a program
that used such loops on 4.77 MHz machines.
The last time I did it was on a 300 MHz machine.
Start.
Blur.
Game over.
Fun to watch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top