Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

time

Posted on 2003-03-20
8
Medium Priority
?
236 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
  • 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
Industry Leaders: 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!

 
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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

578 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