time

I need a function or something of that sort that can wait for a certian amount of time and not using up alot of resources. I got the popular Pause function.

Public Sub Pause(duration As Long)
    Dim Current As Long
    Current = Timer
    Do Until Timer - Current >= duration
        DoEvents
    Loop
End Sub

This is good but it pauses and it uses up alot of resources while it is waiting.
I would use the timer interval but it doesn't go that long. I'm wanting a function that will wait for like 5 or so mintues. thanks in advance!
nugenoaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trkcorpCommented:
Use the Sleep API:
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

call Sleep(1000) 'will wait 1 second
0
JacamarCommented:
Set timer interval to 60000 (1 min)
dim inI as integer
Private Sub Timer1_Timer()
inI = inI + 1
if inI = 5 then
  ' do stuff'
  inI = 0
end if
End Sub
0
JacamarCommented:
Except that the sleep function freezes your program.  You won't beable to do anything while it is sleeping.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

trkcorpCommented:
True, Sleep suspends your execution thread... using 0 resources.
0
saurabhguptaCommented:
Hi trkcorp,
From MSDN:
"You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than Sleep."


Here is MsgWaitForMultipleObjects example code:
'==============================================
Option Explicit

Private Type SECURITY_ATTRIBUTES
   nLength As Long
   lpSecurityDescriptor As Long
   bInheritHandle As Long
End Type

Private Const WAIT_FAILED = &HFFFFFFFF       'Our WaitForSingleObject failed to wait and returned -1
Private Const WAIT_OBJECT_0 = &H0&           'The waitable object got signaled '
Private Const WAIT_ABANDONED = &H80&         'We got out of the waitable object
Private Const WAIT_TIMEOUT = &H102&          'the interval we used, timed out.

'Message constants
Private Const QS_HOTKEY = &H80
Private Const QS_KEY = &H1
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_MOUSEMOVE = &H2
Private Const QS_PAINT = &H20
Private Const QS_POSTMESSAGE = &H8
Private Const QS_TIMER = &H10
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private Const QS_ALLEVENTS = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)

Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long

Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Private Declare Function SetEvent Lib "kernel32" (ByVal hEvent As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


'This routine waits for a specified time without blocking events
Private Function MsgWait(ByVal lngSec As Integer) As Long
   'This code creates an event
   Dim sd As SECURITY_ATTRIBUTES
   With sd
      .nLength = Len(sd)       'we pass the length of sd
      .lpSecurityDescriptor = 0
      .bInheritHandle = 0
   End With
   
   
   Dim WaitRet As Long
   Dim StartTime As Long
   '==Start wait loop==
   StartTime = CLng(Timer)
   Do Until Timer > StartTime + lngSec
       WaitRet = MsgWaitForMultipleObjects(0, 0, False, 100, QS_ALLEVENTS)
       
       Select Case WaitRet
       Case WAIT_TIMEOUT       'The first case must always be WAIT_TIMEOUT 'cause it is the most used option
           DoEvents            'until the shelled process terminates
       Case WAIT_FAILED Or WAIT_ABANDONED
           MsgBox "Wait failed or abandoned", vbExclamation
           Exit Do
       Case WAIT_OBJECT_0      'The first event got signaled so get out of the loop
           DoEvents
       Case Else
           Debug.Assert False
       End Select
   Loop
   '==End wait loop==
   
   MsgWait = WaitRet
End Function
'=============================================

Example:
    MsgWait 5 'Wait for 5 seconds


Regards
Saurabh
0
trkcorpCommented:
COOL!
0
saurabhguptaCommented:
Sorry, I put in some unnecessary code in the previous one. This is all that is required:

'=============================================
Private Const WAIT_FAILED = &HFFFFFFFF       'Our WaitForSingleObject failed to wait and returned -1
Private Const WAIT_OBJECT_0 = &H0&           'The waitable object got signaled '
Private Const WAIT_ABANDONED = &H80&         'We got out of the waitable object
Private Const WAIT_TIMEOUT = &H102&          'the interval we used, timed out.

'Message constants
Private Const QS_HOTKEY = &H80
Private Const QS_KEY = &H1
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_MOUSEMOVE = &H2
Private Const QS_PAINT = &H20
Private Const QS_POSTMESSAGE = &H8
Private Const QS_TIMER = &H10
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private Const QS_ALLEVENTS = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)

Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long


'This routine waits for a specified time without blocking events
Private Function MsgWait(ByVal lngSec As Integer) As Long
   Dim WaitRet As Long
   Dim StartTime As Long
   '==Start wait loop==
   StartTime = CLng(Timer)
   Do Until Timer > StartTime + lngSec
       WaitRet = MsgWaitForMultipleObjects(0, 0, False, 100, QS_ALLEVENTS)
       
       Select Case WaitRet
       Case WAIT_TIMEOUT       'The first case must always be WAIT_TIMEOUT 'cause it is the most used option
           DoEvents            'until the shelled process terminates
       Case WAIT_FAILED Or WAIT_ABANDONED
           MsgBox "Wait failed or abandoned", vbExclamation
           Exit Do
       Case WAIT_OBJECT_0      'The first event got signaled so get out of the loop
           DoEvents
       Case Else
           Debug.Assert False
       End Select
   Loop
   '==End wait loop==
   
   MsgWait = WaitRet
End Function

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nugenoaAuthor Commented:
Cool thanks bro
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.