Advertisement
ASP_Volume2 VB function enhancement #31291

StopWatch Class Module

I bet you're one of those programmers that want to time something in your code, aren't you? Well, with this code you can! This, is a Stopwatch! Not one of those that screw will up when midnight occurs while you're timing. This is... no, not Y2K compliant, infact, it's midnight-compliant! (Fully documented code!)

AI

Resumo por IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Código fonte
original-source
'***************************************************************************
'*PUT THE FOLLOWING INTO A CLASS MODULE. NAME THE CLASS MODULE "CStopWatch"*
'***************************************************************************
Private m_StartTime As Single
Private m_StopTime As Single
Const cSecsInDay As Long = 86400
Public Enum cPauseConstants 'I'm not gonna explain this, consult VB Help if you want to know what it does
  cSeconds = 0
  cMinutes = 1
  cHours = 2
End Enum
Public Sub StartTiming()
  m_StartTime = Timer
End Sub
Public Sub StopTiming()
  m_StopTime = Timer
End Sub
Public Function TimeElapsed() As Single
  
  Dim tempTimeElapsed
  
  tempTimeElapsed = m_StopTime - m_StartTime 'see how many seconds passed since stopwatch has started
  
  If tempTimeElapsed < 0 Then 'if value of above is less than 0, assume that timing started before midnight and ended after midnight
  
    TimeElapsed = tempTimeElapsed + cSecsInDay 'add number of seconds in a day to the negative number and you have the time that has elapsed
   
   Else 'if it's a positive number...
    
    TimeElapsed = tempTimeElapsed
  
  End If
  
End Function
'****************************************************************************
'*To use the functions in your program, paste the following code into a form*
'****************************************************************************
'This goes in the Declaration Section
Dim TimeKeeper as CStopWatch
'Press command1 to start timing
Private Sub Command1_Click()
  Set TimeKeeper = New CStopWatch
  TimeKeeper.StartTiming
End Sub

'Press command2 to stop timing
Private Sub Command2_Click()
  TimeKeeper.StopTiming
End Sub

'Press command3 to display the number of seconds that have elapsed, in a MsgBox
Private Sub Command3_Click()
  Dim Elapsed as Single
  
  Elapsed = TimeKeeper.TimeElapsed
  MsgBox Elapsed
End Sub
'Please give comments and suggestions on this code. It's basically my first
'class module. Email me at: <[email protected]>
Comentários originais (3)
Recuperado do Wayback Machine