NetMQTimer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using NetMQ.Core.Utils;
  3. namespace NetMQ
  4. {
  5. /// <summary>
  6. /// Class NetMQTimerEventArgs is an EventArgs that contains a reference to a NetMQTimer.
  7. /// </summary>
  8. public class NetMQTimerEventArgs : EventArgs
  9. {
  10. /// <summary>
  11. /// Create a new NetMQTimerEventArgs that contains a reference to the given NetMQTimer.
  12. /// </summary>
  13. /// <param name="timer">the NetMQTimer to hold a reference to</param>
  14. public NetMQTimerEventArgs(NetMQTimer timer)
  15. {
  16. Timer = timer;
  17. }
  18. /// <summary>
  19. /// Get the NetMQTimer that this has a reference to.
  20. /// </summary>
  21. public NetMQTimer Timer { get; }
  22. }
  23. /// <summary>
  24. /// A NetMQTimer instance provides the state-information for a timer function,
  25. /// which is periodically checked by a Poller or a NetMQBeacon.
  26. /// </summary>
  27. public class NetMQTimer
  28. {
  29. /// <summary>
  30. /// A pre-constructed NetMQTimerEventArgs to use whenever raising the Elapsed event.
  31. /// </summary>
  32. private readonly NetMQTimerEventArgs m_timerEventArgs;
  33. /// <summary>
  34. /// This is the timer-interval in milliseconds.
  35. /// </summary>
  36. private int m_interval;
  37. /// <summary>
  38. /// This flag dictates whether this timer is currently enabled.
  39. /// </summary>
  40. private bool m_enable;
  41. /// <summary>
  42. /// This event is used to signal when the timer has expired.
  43. /// </summary>
  44. public event EventHandler<NetMQTimerEventArgs>? Elapsed;
  45. /// <summary>
  46. /// Create a new NetMQTimer with the timer-interval specified by the given TimeSpan.
  47. /// </summary>
  48. /// <param name="interval">a TimeSpan that denotes the timer-interval</param>
  49. /// <remarks>
  50. /// This sets the When property to an initial value of -1, to indicate it no future-time applies as yet.
  51. /// </remarks>
  52. public NetMQTimer(TimeSpan interval)
  53. : this((int)interval.TotalMilliseconds)
  54. { }
  55. /// <summary>
  56. /// Create a new NetMQTimer with the given timer-interval in milliseconds.
  57. /// </summary>
  58. /// <param name="interval">an integer specifying the timer-interval in milliseconds</param>
  59. /// <remarks>
  60. /// This sets the When property to an initial value of -1, to indicate it no future-time applies as yet.
  61. /// </remarks>
  62. public NetMQTimer(int interval)
  63. {
  64. m_interval = interval;
  65. m_timerEventArgs = new NetMQTimerEventArgs(this);
  66. m_enable = true;
  67. When = -1;
  68. }
  69. /// <summary>
  70. /// Get or set the timer-interval, in milliseconds.
  71. /// </summary>
  72. /// <remarks>
  73. /// When setting this, When is set to the future point in time from now at which the interval will expire (or -1 if not Enabled).
  74. /// </remarks>
  75. public int Interval
  76. {
  77. get => m_interval;
  78. set
  79. {
  80. m_interval = value;
  81. When = Enable ? Clock.NowMs() + Interval : -1;
  82. }
  83. }
  84. /// <summary>
  85. /// Get or set whether this NetMQTimer is on.
  86. /// </summary>
  87. /// <remarks>
  88. /// When setting this to true, When is set to the future point in time from now at which the interval will expire.
  89. /// When setting this to false, When is set to -1.
  90. /// </remarks>
  91. public bool Enable
  92. {
  93. get => m_enable;
  94. set
  95. {
  96. if (m_enable == value)
  97. return;
  98. m_enable = value;
  99. When = m_enable ? Clock.NowMs() + Interval : -1;
  100. }
  101. }
  102. /// <summary>
  103. /// Get or set the value of the low-precision timestamp (a value in milliseconds) that signals when the timer is to expire,
  104. /// or -1 if not applicable at this time.
  105. /// </summary>
  106. internal long When { get; set; }
  107. /// <summary>
  108. /// Enable the timer and reset the interval
  109. /// </summary>
  110. public void EnableAndReset()
  111. {
  112. if (!Enable)
  113. Enable = true;
  114. else
  115. When = Clock.NowMs() + Interval;
  116. }
  117. /// <summary>
  118. /// If there are any subscribers - raise the Elapsed event.
  119. /// </summary>
  120. /// <param name="sender">the sender to include within the event's event-args</param>
  121. internal void InvokeElapsed(object sender)
  122. {
  123. Elapsed?.Invoke(sender, m_timerEventArgs);
  124. }
  125. }
  126. }