How to delay (1 Viewer)

davesmith202

Employee of Access World
Local time
Today, 02:14
Joined
Jul 20, 2001
Messages
522
How can I have a pause in my code for say half a second??
 
Last edited:

ghudson

Registered User.
Local time
Yesterday, 21:14
Joined
Jun 8, 2002
Messages
6,195
Sometimes the Sleep API does not work as expected like if you need to update something on the form so I use the following Pause() function I have previously posted.

Code:
Public Function Pause(NumberOfSeconds As Variant)
On Error GoTo Err_Pause
    
    Dim PauseTime As Variant, Start As Variant
    
    PauseTime = NumberOfSeconds
    Start = Timer
    Do While Timer < Start + PauseTime
    DoEvents
    Loop
    
Exit_Pause:
    Exit Function
    
Err_Pause:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_Pause
    
End Function
You can now vary the seconds of the pause by calling it like this...

Code:
Pause (.5) 'for a half second pause

HTH
 

Meltdown

Registered User.
Local time
Today, 02:14
Joined
Feb 25, 2002
Messages
472
This is what's great about the internet - ghudson, it's more than a year later, but your little bit of code saved me a lot of time today, thanks a lot for sharing.

Regards
Meltdown
 

mineeksubo

New member
Local time
Yesterday, 18:14
Joined
Jun 14, 2007
Messages
1
The solution by ghudson's is definitely effective. However, I found it comes at the price of high CPU load.

Using the Performance tab on Task Manager, I found my (one core) CPU was effectively at 100% for the entire duration of the specified pause time.

Another solution uses an API declaration, without the performance hit: Sleep.

Here's the sample code (original source unknown):

Code:
    Option Compare Database
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Sub Wait(dblSeconds As Double)
        For i = 1 To dblSeconds * 100
            DoEvents      ' handle events
            Sleep (10)    ' suspend process without a performance hit
        Next
    End Sub

Mike A
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:14
Joined
Sep 12, 2006
Messages
15,634
davesmith

why do you want to pause for half a second

if you are waiting for something to complete, there are other perhaps more reliable techniques available
 

Dudley

Registered User.
Local time
Yesterday, 18:14
Joined
Apr 7, 2004
Messages
147
I'm using the Sleep command to buy time between printing events while BullZip merges the current report with the previous ones (I need to bunch up to 43 separately created report pages into one pdf file). As the merge file grows, Bullzip takes longer to add the new page to the old ones in the merge file, and I was starting to get Bullzip errors because it was already trying to append the next report while it was still working on the last one. So I am trying to use Sleep to pause between instances of sending reports to the Bullzip printer. I'm using a counter to increase the delay as the number of print jobs grows higher. The thing is, the Access screen goes blank white during the sleep cycles and it's definitely disturbing/distracting to the user. I've tried "Application.Echo False" before the sleep line but it doesn't seem to help. I'm concerned about GHudson's Pause function because of the processor resources it uses. I'm afraid it would adversely affect my users who are working in the Terminal Server environment. Does anyone have any suggestions?
 

Dudley

Registered User.
Local time
Yesterday, 18:14
Joined
Apr 7, 2004
Messages
147
Thanks RG. The "API for Sleeping" seems to work great in practice, when testing the code given. When I try to use sSleep, though, I still get the white screen. I think it's related to "opening" the report as a part of the printing process and i don't know how to identify it (trap it?) to use the "Shell and Wait functions". I've commented out as many form changes as I can, as well as "Applicaion.Echo" statements as well, but it doesn't seem to help. I don't know what to do.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:14
Joined
Sep 12, 2006
Messages
15,634
not sure how your bullzip works -

cant you save all your pdfs to a temporary file, then zip them all in one go, and then either delete them or omve them to their final destination - rather than adding them one at a time?
 

darbid

Registered User.
Local time
Today, 03:14
Joined
Jun 26, 2008
Messages
1,428
This thread interests me greatly.

I have been using the Pause function but now after testing see that it increases my CPU up to 50% while it is pausing.

The Sleep function only increases by about 10%.

Thank you very much.

I hope that this is not hyjacking of a thread, if it is I am happy to start a new one.

But as there was the question here "why do you want to pause" I thought I would ask for peoples thoughts on my reason to pause as I really do not know.

I am calling a Shell function to install a webbrowser plugin. "/S /v/qn" makes it silent and the "vbHide" of course hides it.

Code:
Call Shell(ProfileGetItem("GSETTINGS", "world_plugindirectory", sDefValue, sInifile) & _
            "GoogleEarth-Win-Plugin-5.0.11655.6079.exe /S /v/qn", vbHide)
I need to check when it is completed so I have the following;

As Shell is totally silent I know where it will be saved. I check this path every 10 seconds 5 times.

Is this OK? With the Sleep instead of the Pause I am noticing a huge reduction in CPU usage.

Code:
Do While Not fso.folderexists(Environ("PROGRAMFILES") & "\Google\Google Earth Plugin")
        DoEvents
        Sleep (10000)
        DoEvents
        If x = 5 Then
            MsgBox "Failed to install", vbCritical
            Set fso = Nothing
            Exit Sub
        End If
        x = x + 1
    Loop
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:14
Joined
Sep 12, 2006
Messages
15,634
instread of just shell, you can instead try to use a "shell and wait" function. I think most windows processes have a status that indicates they have completed.

So rather than pause access for a specific time (which STILL might not be enough), its better to find a way to let the os tell you it has finished.

alternatively, say your external process creates a file...

then in your access app you can say something like

Code:
while not exists(expectedfilename)
  doevents
wend

which waits for the file to be created - but while it is waiting, it lets you do other things in access
 

Dudley

Registered User.
Local time
Yesterday, 18:14
Joined
Apr 7, 2004
Messages
147
not sure how your bullzip works -

cant you save all your pdfs to a temporary file, then zip them all in one go, and then either delete them or omve them to their final destination - rather than adding them one at a time?

Thanks Dave. that's a good idea. I'll look into how Bullzip does it. I think PDF_Creator does it that way, or can, but I don't know about Bullzip. There used to be problems with PDF_Creator in Vista. I've posted on the Bullzip forum to see if there's a way to know when the merge process has ended, to see if I can do it that way. I'll post back.
 

Dudley

Registered User.
Local time
Yesterday, 18:14
Joined
Apr 7, 2004
Messages
147
No help so far in finding a way in Bullzip to determine whether a merge process has completed. Is there a way to simply determine whether a file is "open" or "in use" on the computer?
 

Dudley

Registered User.
Local time
Yesterday, 18:14
Joined
Apr 7, 2004
Messages
147
So, my latest thought is to try to use the Windows Task Manager to see if Bullzip is still running before moving on to the next report. I've worked out part of the Tasklist command as follows:

Tasklist /v /fi “Windowtitle eq Bullzip PDF Printer” /fi “STATUS eq running”

but I'm not sure how to implement it. I think this line causes a printout of Task Manager members meeting the criteria. If that's right, once I get it going, I can make it work that if the output file exists, don't continue. Pseudocode something like:

  1. Run Tasklist command
  2. Check for file
  3. If not found, exit loop, proceed with next report
  4. If found, Delete file, Loop again

Can anyone point me in the direction of actually running the tasklist command from Access?

Should I somehow open this to a new thread? If so, how do I do that?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:14
Joined
Sep 12, 2006
Messages
15,634
try googling shell and wait

i'm sure most processes return a code to confirm they have completed.
 

darbid

Registered User.
Local time
Today, 03:14
Joined
Jun 26, 2008
Messages
1,428
Finding out what bullzip is doing

I may be off track here but can you set bullzip to "end" after finishing its work?

If so why not check if bullzip is closed by trying to find its Class and Caption.

Here is a thread where I asked about this for adobe (but the reverse, i wanted to know when it had loaded)

http://www.access-programmers.co.uk/forums/showthread.php?t=154403
 

Users who are viewing this thread

Top Bottom