Option Explicit
'J.P Novembre 2024
#If VBA7 Then
Private Declare PtrSafe Function QueryPerformanceFrequency Lib "kernel32" (X As Currency) As Boolean
Private Declare PtrSafe Function QueryPerformanceCounter Lib "kernel32" (X As Currency) As Boolean
#Else
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (X As Currency) As Boolean
Private Declare Function QueryPerformanceCounter Lib "kernel32" (X As Currency) As Boolean
#End If
Public Event EvFin()
Public Event EvDepart()
Public Event EvChrono(ByVal Temps As Double)
Private WithEvents Ryth As Rythmeur, Dep As Double, Fin As Double, TimeOut As Double, Freq As Long
Private Fréq As Currency, CycBase As Currency, Cyc As Currency, CycCum As Currency, Temps As Double
Private BDepart As Boolean, BFin As Boolean
Private Sub Class_Initialize()
QueryPerformanceCounter CycBase: QueryPerformanceFrequency Fréq
Set Ryth = New Rythmeur
End Sub
Public Sub Init(ByVal TempsDépart As Double, ByVal Durée As Double, _
ByVal Précision As Integer, ByVal Limite As Double)
Freq = Précision
Dep = TempsDépart
Fin = TempsDépart + Durée
TimeOut = Limite
QueryPerformanceCounter Cyc: If Lancé Then Exit Sub
If Cyc - CycCum - CycBase > Freq / 10000@ Then CycBase = Cyc - CycCum
BDepart = False: BFin = False
Ryth.Lancer Freq
End Sub
Private Sub Ryth_Intervient(ByVal Tic As Long)
On Error Resume Next
QueryPerformanceCounter Cyc: CycCum = Cyc - CycBase
Temps = CycCum / Fréq + 0.05: RaiseEvent EvChrono(Temps)
If Temps > Dep And Not BDepart Then
BDepart = True
RaiseEvent EvDepart
End If
If Temps > Fin And Not BFin Then
RaiseEvent EvFin
BFin = True
Ryth.Stopper
End If
If Temps > TimeOut Then
Ryth.Stopper
Set Ryth = Nothing
Debug.Print "TimeOut"
End If
End Sub
Public Sub Stopper()
Ryth.Stopper
End Sub
Public Function Lancé() As Boolean
Rem. ——— Propriété en lecture seule. Indique si le chronomètre est lancé.
Lancé = Ryth.Lancé
End Function