Timer (Component) der synchron zur Systemuhr tickt

    • VB.NET

    Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

      Timer (Component) der synchron zur Systemuhr tickt

      Der Titel sagt eigentlich alles.
      Dieser Timer tickt halbwegs synchron zur Systemuhr, und zwar zur jeden vollen Sekunde.

      VB.NET-Quellcode

      1. Imports System.ComponentModel
      2. ''' <summary>
      3. ''' Synchronized to the system clock, ticks every full second.
      4. ''' </summary>
      5. <ToolboxItem(True)>
      6. <ToolboxBitmap(GetType(Timer), "System.Windows.Forms.Timer.bmp")>
      7. Public Class SystemClockTimer
      8. Inherits Timer
      9. ''' <summary>
      10. ''' Will be calculated automatically.
      11. ''' </summary>
      12. ''' <returns>
      13. ''' Calculated milliseconds as Integer.
      14. ''' </returns>
      15. <Browsable(False)>
      16. <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
      17. <EditorBrowsable(EditorBrowsableState.Never)>
      18. Public Shadows ReadOnly Property Interval() As Integer
      19. Get
      20. Return Math.Max(1, 1000 - Date.Now.Millisecond)
      21. End Get
      22. End Property
      23. Public Overloads Property Enabled() As Boolean
      24. Get
      25. Return MyBase.Enabled
      26. End Get
      27. Set(value As Boolean)
      28. MyBase.Enabled = value
      29. If value Then
      30. MyBase.Interval = Interval
      31. MyBase.Start()
      32. Else
      33. MyBase.Stop()
      34. End If
      35. End Set
      36. End Property
      37. Public Shadows Event Tick(sender As Object, e As SystemClockTimerEventArgs)
      38. Private Sub MyBase_Tick(sender As Object, e As EventArgs) Handles MyBase.Tick
      39. RaiseEvent Tick(sender, New SystemClockTimerEventArgs())
      40. MyBase.Interval = Interval
      41. End Sub
      42. Public Overloads Sub Start()
      43. Enabled = True
      44. End Sub
      45. Public Overloads Sub [Stop]()
      46. Enabled = False
      47. End Sub
      48. End Class
      49. Public Class SystemClockTimerEventArgs
      50. Inherits EventArgs
      51. Public ReadOnly Property Now As Date
      52. Sub New()
      53. Now = Date.Now
      54. End Sub
      55. End Class


      Anwendungsbeispiel:

      VB.NET-Quellcode

      1. Private Sub SystemClockTimer1_Tick(sender As Object, e As SystemClockTimerEventArgs) Handles SystemClockTimer1.Tick
      2. LabelClock.Text = e.Now.ToLocalTime.ToLongTimeString
      3. End Sub
      Vereinfachungen:

      VB.NET-Quellcode

      1. Imports System.ComponentModel
      2. ''' <summary>Synchronized to the system clock, ticks every full second.</summary>
      3. <ToolboxItem(True)>
      4. <ToolboxBitmap(GetType(Timer), "System.Windows.Forms.Timer.bmp")>
      5. Public Class SystemClockTimer : Inherits Timer
      6. ''' <summary>calculated automatically in milliseconds, Attributes hide it to Users</summary>
      7. <Browsable(False)>
      8. <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
      9. <EditorBrowsable(EditorBrowsableState.Never)>
      10. Public Shadows ReadOnly Property Interval() As Integer
      11. Get
      12. Return Math.Max(1, 1000 - Date.Now.Millisecond)
      13. End Get
      14. End Property
      15. Public Overrides Property Enabled() As Boolean
      16. Get
      17. Return MyBase.Enabled
      18. End Get
      19. Set(value As Boolean)
      20. If value = MyBase.Enabled Then Return
      21. If value Then MyBase.Interval = Interval
      22. MyBase.Enabled = value
      23. End Set
      24. End Property
      25. Protected Overrides Sub OnTick(e As EventArgs)
      26. MyBase.OnTick(e)
      27. MyBase.Interval = Interval
      28. End Sub
      29. End Class
      Statt Shadowing ist Overriding anwendbar, insbesondere auch beim Event (Event/OnEvent-Override - Pattern). Dadurch kann das bestehende Event weiter-benutzt werden - ein eigenes braucht nicht neu erfunden zu werden.

      Aufruf

      VB.NET-Quellcode

      1. Private Sub SystemClockTimer1_Tick(sender As Object, e As EventArgs) Handles SystemClockTimer1.Tick
      2. LabelClock.Text = Date.Now.ToLocalTime.ToLongTimeString
      3. End Sub

      Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „ErfinderDesRades“ ()