Posted By

mafro on 05/29/07


Tagged

precision timer


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

haoji


Precision Timer


 / Published in: VB.NET
 

Countdown timer with 100 millisecond precision. Raises event every second.

  1. Imports System.Timers
  2.  
  3. Public Class StopWatch
  4.  
  5. Private WithEvents timer As Timers.Timer 'use system timer
  6.  
  7. Private _StartTime As Date
  8. Private _CurrentTime As Date
  9. Private _TargetTime As Date
  10.  
  11. Public Delegate Sub ElapsedEventHandler(ByVal sender As System.Object, ByVal e As ElapsedEventArgs)
  12. Public Event Elapsed As ElapsedEventHandler
  13.  
  14.  
  15. Public ReadOnly Property ElapsedTime() As TimeSpan
  16. Get
  17. Return Me._CurrentTime.Subtract(Me._StartTime)
  18. End Get
  19. End Property
  20.  
  21. Public ReadOnly Property RemainingTime() As TimeSpan
  22. Get
  23. If Me._TargetTime = Nothing Then
  24. Return Nothing
  25. ElseIf TimeSpan.Compare(Me._TargetTime.Subtract(Me._CurrentTime), TimeSpan.Zero) = -1 Then
  26. Return TimeSpan.Zero
  27. Else
  28. Return Me._TargetTime.Subtract(Me._CurrentTime)
  29. End If
  30. End Get
  31. End Property
  32.  
  33.  
  34. Public Sub SetTargetTime(ByVal mins As Double)
  35. Me._TargetTime = Me._StartTime.AddMinutes(mins)
  36. End Sub
  37.  
  38.  
  39. Public Sub New()
  40. 'create timer object
  41. Me.timer = New Timers.Timer()
  42.  
  43. 'initialise internal timer
  44. Me._StartTime = Date.Now
  45. Me._CurrentTime = Me._StartTime
  46. End Sub
  47.  
  48. Public Sub Start()
  49. Me.PrevTime = Now 'initialise timer
  50. Me.timer.Start()
  51. End Sub
  52.  
  53. Public Sub [Stop]()
  54. Me.timer.Stop()
  55. End Sub
  56.  
  57. Public Sub Reset()
  58. Me.Stop()
  59.  
  60. 'recreate timer object
  61. Me.timer = New Timers.Timer()
  62.  
  63. 'initialise internal timer
  64. Me._StartTime = Date.Now
  65. Me._CurrentTime = Me._StartTime
  66. End Sub
  67.  
  68.  
  69. Private PrevTime As Date 'record real time for diff
  70. Private DiffTime As TimeSpan
  71.  
  72. Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As Timers.ElapsedEventArgs) Handles timer.Elapsed
  73. 'retrieve time diff from last tick
  74. Me.DiffTime = e.SignalTime.Subtract(Me.PrevTime)
  75.  
  76. 'increment internal timer
  77. Me._CurrentTime = Me._CurrentTime.Add(Me.DiffTime)
  78.  
  79. 'check reached target time
  80. If Not Me._TargetTime = Nothing Then
  81. If Me._CurrentTime >= Me._TargetTime Then Me.Stop()
  82. End If
  83.  
  84. 'record event time for next tick
  85. Me.PrevTime = e.SignalTime
  86.  
  87. 'raise event to main form
  88. RaiseEvent Elapsed(sender, New ElapsedEventArgs(Me._CurrentTime))
  89. End Sub
  90.  
  91. End Class
  92.  
  93.  
  94. Public Class ElapsedEventArgs
  95. Inherits EventArgs
  96.  
  97. Private _SignalTime As DateTime
  98.  
  99. Public ReadOnly Property SignalTime As DateTime
  100. Get
  101. Return _SignalTime
  102. End Get
  103. End Property
  104.  
  105. Friend Sub New(ByVal SignalTime As DateTime)
  106. _SignalTime = SignalTime
  107. End Sub
  108. End Class

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: tonyhug on October 9, 2008

Hi,

I am currently writing a VB application where timing is important (milliseconds accuracy needed - not Micro), and this looks like a very useful example of code. However, as a relative newcomer to VB.net I am having a problem with the sub:

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As Timers.ElapsedEventArgs) Handles Timer.Elapsed

the actual problem is with this part:

'raise event to main form RaiseEvent Elapsed(sender, New Mafro.ElapsedEventArgs(Me._CurrentTime))

Visual Studio is giving me a compile time error when i try and build it. My Main form is form1 (default) and I have set a public alias to it in the file containg the StopWatch class:

public Mafro as new form1

I am getting : Type 'Mafro.ElapsedEventArg' is not defined

Any assistance ould be very elcomed.

Thanks.

Posted By: tonyhug on October 10, 2008

Hi,

Please ignore the previous comment. I removed the "mafro" from RaiseEvent Elapsed(sender, New Mafro.ElapsedEventArgs(Me._CurrentTime))

And all worked perfectly.

Thanks.

You need to login to post a comment.