Guest User

Untitled

a guest
Jun 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. Public Class ImprovedTimer
  2. Inherits System.Timers.Timer
  3.  
  4. Private _sw As System.Diagnostics.Stopwatch
  5. Private _paused As Boolean
  6. Private _originalInterval As Double
  7. Private _intervalRemaining As Double?
  8.  
  9. Public ReadOnly Property IntervalRemaining() As Double?
  10. Get
  11. Return _intervalRemaining
  12. End Get
  13. End Property
  14.  
  15. Public ReadOnly Property Paused() As Boolean
  16. Get
  17. Return _paused
  18. End Get
  19. End Property
  20.  
  21. Public ReadOnly Property OriginalInterval() As Double
  22. Get
  23. Return _originalInterval
  24. End Get
  25. End Property
  26.  
  27. Public Sub Pause()
  28. If Me.Enabled Then
  29. _intervalRemaining = Me.Interval - _sw.ElapsedMilliseconds
  30. _paused = True
  31. resetStopWatch(False, False)
  32. MyBase.Stop()
  33. End If
  34. End Sub
  35.  
  36. Public Sub [Resume]()
  37. If _paused Then
  38. Me.Interval = If(_intervalRemaining.HasValue, _intervalRemaining.Value, _originalInterval)
  39. resetStopWatch(True, False)
  40. MyBase.Start()
  41. End If
  42. End Sub
  43.  
  44. Public Overloads Property Enabled() As Boolean
  45. Get
  46. Return MyBase.Enabled
  47. End Get
  48. Set(ByVal value As Boolean)
  49. MyBase.Enabled = value
  50. resetStopWatch(MyBase.Enabled, True)
  51. End Set
  52. End Property
  53.  
  54. Public Overloads Sub Start()
  55. resetStopWatch(True, True)
  56. MyBase.Start()
  57. End Sub
  58.  
  59. Public Overloads Sub [Stop]()
  60. resetStopWatch(False, True)
  61. MyBase.Stop()
  62. End Sub
  63.  
  64. Public Overloads Property Interval() As Double
  65. Get
  66. Return MyBase.Interval
  67. End Get
  68. Set(ByVal value As Double)
  69. MyBase.Interval = value
  70. If Not _paused Then
  71. _originalInterval = MyBase.Interval
  72. End If
  73. End Set
  74. End Property
  75.  
  76. Private Sub resetStopWatch(ByVal startNew As Boolean, ByVal resetPause As Boolean)
  77. If _sw IsNot Nothing Then
  78. _sw.Stop()
  79. _sw = Nothing
  80. End If
  81. If resetPause Then
  82. If _paused Then
  83. Me.Interval = _originalInterval
  84. End If
  85. _paused = False
  86. _intervalRemaining = Nothing
  87. End If
  88. If startNew Then
  89. _sw = System.Diagnostics.Stopwatch.StartNew
  90. End If
  91. End Sub
  92.  
  93. Private Sub ImprovedTimer_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
  94. resetStopWatch(False, True)
  95. End Sub
  96.  
  97. Private Sub ImprovedTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Me.Elapsed
  98. resetStopWatch(Me.AutoReset, True)
  99. End Sub
  100.  
  101. End Class
  102.  
  103. timer.Change( Timeout.Infinite, Timeout.Infinite );
  104.  
  105. Me.Interval = Me.Interval - sw.ElapsedMilliseconds
Add Comment
Please, Sign In to add comment