Advertisement
TizzyT

ElapseManager -TizzyT

Sep 28th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.49 KB | None | 0 0
  1. Public Class ElapseManager
  2.     Public Shared ReadOnly TicksPerMillisecond As Long = Stopwatch.Frequency / 1000
  3.     Private ReadOnly ElapseCollection As New Dictionary(Of String, Elapser)
  4.  
  5.     Public Function ChangeInterval(ByVal Name As String, ByVal Ticks As Long) As Boolean
  6.         If ElapseCollection.ContainsKey(Name) Then
  7.             ElapseCollection(Name).Interval = Ticks
  8.             Return True
  9.         End If
  10.         Return False
  11.     End Function
  12.  
  13.     Public Function ChangeInterval(ByVal Name As String, ByVal Milliseconds As Double) As Boolean
  14.         If ElapseCollection.ContainsKey(Name) Then
  15.             ElapseCollection(Name).Interval = Milliseconds * TicksPerMillisecond
  16.             Return True
  17.         End If
  18.         Return False
  19.     End Function
  20.  
  21.     Public Function Check(ByVal Name As String) As Boolean
  22.         If ElapseCollection.ContainsKey(Name) Then Return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp
  23.         Return False
  24.     End Function
  25.  
  26.     Public Function Check(ByVal Name As String, ByVal Ticks As Long, Optional ByVal TriggerOnFirstCheck As Boolean = True) As Boolean
  27.         If ElapseCollection.ContainsKey(Name) Then
  28.             Return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp
  29.         Else
  30.             If Ticks > 0 Then
  31.                 Dim nextTime As Long = Stopwatch.GetTimestamp + Ticks
  32.                 ElapseCollection.Add(Name, New Elapser(nextTime, Ticks))
  33.                 Return If(TriggerOnFirstCheck, True, ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp)
  34.             Else
  35.                 Throw New Exception("Ticks must be greater than 0" & vbCrLf & vbTab & " - TizzyT")
  36.             End If
  37.         End If
  38.     End Function
  39.  
  40.     Public Function Check(ByVal Name As String, ByVal Milliseconds As Double, Optional ByVal TriggerOnFirstCheck As Boolean = True) As Boolean
  41.         If ElapseCollection.ContainsKey(Name) Then
  42.             Return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp
  43.         Else
  44.             If Milliseconds > 0 Then
  45.                 Dim newTicks As Long = Milliseconds * TicksPerMillisecond
  46.                 Dim nextTime As Long = Stopwatch.GetTimestamp + newTicks
  47.                 ElapseCollection.Add(Name, New Elapser(nextTime, newTicks))
  48.                 Return If(TriggerOnFirstCheck, True, ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp)
  49.             Else
  50.                 Throw New Exception("Milliseconds must be greater than 0" & vbCrLf & vbTab & " - TizzyT")
  51.             End If
  52.         End If
  53.     End Function
  54.  
  55.     Public Function CheckRestart(ByVal Name As String) As Boolean
  56.         If ElapseCollection.ContainsKey(Name) Then
  57.             Dim Elapsed As Boolean = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp
  58.             ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval
  59.             Return Elapsed
  60.         End If
  61.         Return False
  62.     End Function
  63.  
  64.     Public Function CheckRestart(ByVal Name As String, ByVal Ticks As Long, Optional ByVal TriggerOnFirstCheck As Boolean = True) As Boolean
  65.         If ElapseCollection.ContainsKey(Name) Then
  66.             Dim Elapsed As Boolean = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp
  67.             If Elapsed Then ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval
  68.             Return Elapsed
  69.         Else
  70.             If Ticks > 0 Then
  71.                 Dim nextTime As Long = Stopwatch.GetTimestamp + Ticks
  72.                 ElapseCollection.Add(Name, New Elapser(nextTime, Ticks))
  73.                 Return If(TriggerOnFirstCheck, True, ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp)
  74.             Else
  75.                 Throw New Exception("Ticks must be greater than 0" & vbCrLf & vbTab & " - TizzyT")
  76.             End If
  77.         End If
  78.     End Function
  79.  
  80.     Public Function CheckRestart(ByVal Name As String, ByVal Milliseconds As Double, Optional ByVal TriggerOnFirstCheck As Boolean = True) As Boolean
  81.         If ElapseCollection.ContainsKey(Name) Then
  82.             Dim Elapsed As Boolean = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp
  83.             If Elapsed Then ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval
  84.             Return Elapsed
  85.         Else
  86.             If Milliseconds > 0.0 Then
  87.                 Dim newTicks As Long = Milliseconds * TicksPerMillisecond
  88.                 Dim nextTime As Long = Stopwatch.GetTimestamp + newTicks
  89.                 ElapseCollection.Add(Name, New Elapser(nextTime, newTicks))
  90.                 Return If(TriggerOnFirstCheck, True, ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp)
  91.             Else
  92.                 Throw New Exception("Milliseconds must be greater than 0" & vbCrLf & vbTab & " - TizzyT")
  93.             End If
  94.         End If
  95.     End Function
  96.  
  97.     Public Function Remove(ByVal Name As String) As Boolean
  98.         Return ElapseCollection.Remove(Name)
  99.     End Function
  100.  
  101.     Public Function Restart(ByVal Name As String) As Boolean
  102.         If ElapseCollection.ContainsKey(Name) Then
  103.             ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval
  104.             Return True
  105.         End If
  106.         Return False
  107.     End Function
  108.  
  109.     Private Class Elapser
  110.         Public NextTime As Long
  111.         Public Interval As Long
  112.         Sub New(ByVal NextTime As Long, ByVal Interval As Long)
  113.             Me.NextTime = NextTime
  114.             Me.Interval = Interval
  115.         End Sub
  116.     End Class
  117. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement