CPause (Class for pausing)

By | 2002-06-01

This is a class that you can use to make your app pause for a few hours/minutes/seconds or milliseconds. This will work even if midnight occurrs while pausing! (Thus, this is midnight-compliant, just like my CStopwatch class I submitted earlier.)

Original Author: Jan Botha

Inputs

Function pPause(ByVal Number as Single, Optional Byval Unit as iConstants)
Number: the number of units, specified in Unit, that you want to pause
Unit: Contains one of the Following:
iMillisec ( = 3)
iSeconds ( = 0)
iMinutes ( = 1)
iHours ( = 2)
NOTE: If Unit is omitted, Unit will equal iSeconds

Side Effects

Unfortunately, if you want to see if this can pause through midnight, you’ll have to set your system time. Nothing serious. 🙂

Code

'*************************************
'This goes into a class module
'Important: NAME THE MODULE "CPause"
'*************************************
Const iSecsInDay As Long = 86400
Enum iConstants
  iSeconds = 0
  iMinutes = 1
  iHours = 2
  iMilliSec = 3
End Enum
  
Public Function pPause(ByVal Number As Single, _
     Optional ByVal Unit As iConstants)
  Dim iStopTime, fakeTimer, sAfterMidnight, sBeforeMidnight
  If Unit = iSeconds Then
    Number = Number
   ElseIf Unit = iMinutes Then
    Number = Number * 60
   ElseIf Unit = iHours Then
    Number = Number * 3600
   ElseIf Unit = iMilliSec Then
    Number = Number / 1000
  End If
  fakeTimer = Timer
  iStopTime = fakeTimer + Number
  If iStopTime > iSecsInDay Then
    sAfterMidnight = iStopTime - iSecsInDay
    sBeforeMidnight = Number - sAfterMidnight
    fakeTimer = Timer
    While Timer < fakeTimer + sBeforeMidnight And Timer <> 0
      DoEvents
    Wend
    fakeTimer = Timer
    While Timer < fakeTimer + sAfterMidnight
      DoEvents
    Wend
   Else 'if pausing won't continue through midnight
    While Timer < iStopTime
      DoEvents
    Wend
  End If
End Function
'************************************
'Put the following in the Declaration
'section of a form
'************************************
Dim mytimer as CPause
'***************************************************
'Put the following into any Sub (eg. Command1_Click)
'***************************************************
Set mytimer = New CPause
'to pause for 10 seconds, use the following call
i = mytimer.pPause(10, iSeconds)
'**************************************************
'End of Code
'I welcome any comments bug reports or enhancements that can be made!
'

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.