?
Solved

time

Posted on 2003-03-20
8
Medium Priority
?
226 Views
Last Modified: 2010-04-07
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!
0
Comment
Question by:nugenoa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:trkcorp
ID: 8176980
Use the Sleep API:
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

call Sleep(1000) 'will wait 1 second
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8176990
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
 
LVL 2

Expert Comment

by:Jacamar
ID: 8177003
Except that the sleep function freezes your program.  You won't beable to do anything while it is sleeping.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:trkcorp
ID: 8177017
True, Sleep suspends your execution thread... using 0 resources.
0
 
LVL 2

Expert Comment

by:saurabhgupta
ID: 8177034
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
 
LVL 4

Expert Comment

by:trkcorp
ID: 8177053
COOL!
0
 
LVL 2

Accepted Solution

by:
saurabhgupta earned 150 total points
ID: 8177066
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
 

Author Comment

by:nugenoa
ID: 8177092
Cool thanks bro
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question