473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trouble with this WaitFor() procedure...

MLH
This was contributed by someone in this NG
last year...

Sub WaitFor(psngSeconds As Single)
' wait for specified number of seconds
' Copyright Trevor Best (tre...@besty.org.uk) <-OK, so I added
thisline.

Dim sngStart As Single
Dim sngET As Single

sngStart = Timer
DoEvents
Do While sngET < psngSeconds
DoEvents
' check for mighnight as the Timer() function will
' rollover at this point
sngET = Timer - sngStart
If sngET < 0 Then
' it rolled over - add number of seconds
' in a day
sngET = sngET + 86400
Beep
End If
' now don't hog the processor
' release some CPU slices back to Windoze
DoEvents
Loop
End Sub

Trouble is - it doesn't always wait. I've watched it
carefully. Sometimes the actual waiting time is zero.
In spite of the setting of psngSeconds argument.
Anyone know what might be causing this eratic
behaviour?
Jan 23 '07 #1
7 3288
MLH
Stick a command button on a test form with
the following code in it. Open the form & click
the button. If your debug window is open,
you'll see the problem clearly illustrated.
Private Sub Command0_Click()
Commence:
WaitTime = 10 + Int(Rnd * 41)
Debug.Print "Waiting for " & Trim$(CStr(WaitTime)) & " seconds..."
WaitFor (WaitTime)
GoTo Commence
End Sub

Jan 23 '07 #2
MLH
Can anyone tell me why, if you run the code behind
the Form command button described above - why does
the code keep on running even I close the form? The
only way I can stop it is to close the database itself.

I thought opening a form in design view or closing it
would halt the processes running on the form. That
must not be the case.
Jan 23 '07 #3
The VBA While is not waiting for ten seconds. It is working for ten
seconds.

Declare Function Sleep& Lib "kernel32" (ByVal dwMillisecond&)

Private Sub Command0_Click()
Debug.Print Now()
Sleep 10000
Debug.Print Now()
End Sub

What we could hope is that this sleeps more soundly than the VBA
function; that is, the cpu usage is not 100% as it is with a VBA While
Procedure. And, of course, it does not need to check for pumpkins.
Documentation indicates it suspends operations only in the thread
wherein it is called; so the absence of DoEvents !!!!!SHOULD!!!!! not
be a problem.

SleepEx is an advanced function, that when used with ?????FileEx
functions can sleep for the entire time specified but return early if
an input or output operation completes.

Jan 23 '07 #4
MLH
Dev Ashish posted something like this 8-years ago. I haven't
been using it because calls to sapiSleep() sub act more processor
intensive than those to VBA WaitFor() procedure. I'm reading
what you've written below, and it does seem logical. However,
I can grab windows by their title bars and move them around
whilst WaitFor'ing and I can't do that when sapiSleep'ing. Maybe
that seems contradictory to the documentation. But it's True. If
I'm WaitFor'ing say for 30-seconds, I can click the Database
window, open an object in design view then close it. On the other
hand, if I'm sapiSleeping for a similar time period, I cannot select
any other database object during that time. Nor can I grab the
form in which the sapiSleep procedure was Called by its Title Bar
and move around. Am pretty much lock out-a-doing anything
until the sapiSleep time period expires. Why is that?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxx

On 23 Jan 2007 10:08:59 -0800, "Lyle Fairfield"
<ly***********@aim.comwrote:
>The VBA While is not waiting for ten seconds. It is working for ten
seconds.

Declare Function Sleep& Lib "kernel32" (ByVal dwMillisecond&)

Private Sub Command0_Click()
Debug.Print Now()
Sleep 10000
Debug.Print Now()
End Sub

What we could hope is that this sleeps more soundly than the VBA
function; that is, the cpu usage is not 100% as it is with a VBA While
Procedure. And, of course, it does not need to check for pumpkins.
Documentation indicates it suspends operations only in the thread
wherein it is called; so the absence of DoEvents !!!!!SHOULD!!!!! not
be a problem.

SleepEx is an advanced function, that when used with ?????FileEx
functions can sleep for the entire time specified but return early if
an input or output operation completes.
Jan 24 '07 #5
The WaitFor function yields to other processes when it calls DoEvents.
But it is enormously inefficient, looping more than a thousand times
per second of wait (depending on cpu ... on a very fast cpu I suppose
this could approach 100, 000). As well the consecutive DoEvents can
confuse the process so that if we immediately make a subsequent call to
the function, there is no wait at all (as you have noted). Removing one
of the DoEvents solves that problem but it doubles the number of loops
made.

On the bizarre side we could write the function as
Dim z As Long
For z = 0 To 30000
DoEvents
Next z
or whatever number is appropriate for your cpu.

There is no loop in Sleep and, as a result, no where to put a DoEvents
If you modify the size of your Window you will find that you can click
buttons etc in other processes, but not in Access which is the process
that is "asleep".

Trevor has contributed many great functions. WaitFor is not one of
them. If you really must be able to click in (Access) windows while the
process sleeps then you could try something like:

For z = 0 to 9
Sleep 999
Do Events
Next z

Jan 24 '07 #6
MLH
Based on this reply, I think I'll go with the
Sleep'er. Come to think of it, I really don't
need to be monkeying in the Access app
while procedure that is sleeping snoozes.

I would be perfectly happy to work in other
apps during that time. Will give it a try.
Jan 26 '07 #7
MLH
Lyle, what might I do to determine whether
(A) opening a form in dialog mode whose Timer
event property was a procedure that did nothing
but close the form 10 seconds after opening

IS LESS PROCESSOR INTENSIVE THAN

(B) using sapiSleep to effect a similar 10-second
delay in a procedure.

Like this - suppose the following is a snippet from
my procedure

....
code
code
code
more code

now do (A) OR do (B) and determine which places
the least load on the system

code continues
code
code
blah, blah, blah
Jan 26 '07 #8

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

Similar topics

14
by: Des L. Davis | last post by:
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003 Server Software: Microsoft SQL Server 2000 Enterprise running on...
11
by: hendershot | last post by:
Using SQL Server 2000 SP3a, I run the following in 2 query analizer windows on the Northwind database, the second one always gets the deadlock Msg 1205: Window 1: declare @cnt int select...
3
by: NickName | last post by:
Env: SQL Server 2000 It actually waited for 50 seconds instead of 5, tried WAITFOR DELAY '000:00:005' and WAITFOR DELAY '000:00:5' respectively, and got same behavior. Bug or ? TIA
4
by: Driesen via SQLMonster.com | last post by:
Hi guys I having trouble with this sproc. I get the following error when testing: Server: Msg 245, Level 16, State 1, Procedure UTL_CompletenessCheckLoan, Line 231 Syntax error converting the...
0
by: Shanmugasundaram Doraisamy | last post by:
Dear Group, We are using Postgresql 7.3.4 on Redhat 8.0 with Java 1.4.2. We are developing our applications in Java. We call stored procedures from the java program. Order numbers are generated...
2
by: vvenk | last post by:
Hello: I wrote a test procedure on Oracle that returns a string and a value. CREATE OR REPLACE PROCEDURE EBMS.p_CSV_Upload ( P_ERROR OUT VARCHAR2, P_ERROR_NO ...
1
by: alecarnero | last post by:
I have write this procedure ALTER PROCEDURE dbo.proba (@ident uniqueidentifier) AS SELECT ident, nome FROM dbo.nomes WHERE (ident = @ident) Private Sub Form_Open(Cancel As Integer) Dim rsn...
2
by: Zethex | last post by:
At the moment i'm doing a piece of work for school and I'm stuck at the moment. I have a list of words, for example: Sentence = I have another list which I need to use to replace certain...
17
dmjpro
by: dmjpro | last post by:
Have a look at my code snippet. Process l_p = Runtime.getRuntime().exec("my command"); l_p.waitFor(); //here the program hangs out Then i go for a site and found it's solution .. Simply...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.