WayneTimer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Threading;
  3. namespace Wayne.Lib
  4. {
  5. /// <summary>
  6. /// Timer factory
  7. /// </summary>
  8. public class WayneTimerFactory : ITimerFactory
  9. {
  10. /// <summary>
  11. /// Creates a timer
  12. /// </summary>
  13. /// <param name="id"></param>
  14. /// <param name="parentEntity"></param>
  15. /// <param name="name"></param>
  16. /// <returns></returns>
  17. public ITimer Create(int id, IIdentifiableEntity parentEntity, string name)
  18. {
  19. return new WayneTimer(id, parentEntity, name);
  20. }
  21. /// <summary>
  22. /// Creates a timer
  23. /// </summary>
  24. /// <typeparam name="TState"></typeparam>
  25. /// <param name="id"></param>
  26. /// <param name="parentEntity"></param>
  27. /// <param name="name"></param>
  28. /// <param name="state"></param>
  29. /// <returns></returns>
  30. public ITimer<TState> Create<TState>(int id, IIdentifiableEntity parentEntity, string name, TState state)
  31. {
  32. return new WayneTimer<TState>(id, parentEntity, name, state);
  33. }
  34. }
  35. /// <summary>
  36. /// Changes the due time and interval of a timer.
  37. /// </summary>
  38. internal abstract class WayneTimerChanger : ITimerChanger
  39. {
  40. private bool disposed;
  41. private readonly Timer timer;
  42. protected WayneTimerChanger(int id, IIdentifiableEntity parentEntity, string name, object state)
  43. {
  44. Id = id;
  45. ParentEntity = parentEntity;
  46. EntitySubType = name;
  47. timer = new Timer(TimerCallback, state, Timeout.Infinite, Timeout.Infinite);
  48. }
  49. ~WayneTimerChanger()
  50. {
  51. Dispose(false);
  52. }
  53. public void Dispose()
  54. {
  55. Dispose(true);
  56. GC.SuppressFinalize(this);
  57. }
  58. private void Dispose(bool disposing)
  59. {
  60. if (disposed)
  61. return;
  62. disposed = true;
  63. if (disposing)
  64. timer.Dispose();
  65. }
  66. public int Id { get; private set; }
  67. public string EntityType { get { return "WayneTimer"; } }
  68. /// <summary>
  69. /// This is used by the logger and should never be set by inheriting classes
  70. /// </summary>
  71. public string FullEntityName { get; set; }
  72. public string EntitySubType { get; private set; }
  73. public IIdentifiableEntity ParentEntity { get; private set; }
  74. public bool Change(TimeSpan? dueTime, TimeSpan? period)
  75. {
  76. if (!dueTime.HasValue || !period.HasValue)
  77. {
  78. return timer.Change(dueTime.HasValue ? (long)dueTime.Value.TotalMilliseconds : Timeout.Infinite,
  79. period.HasValue ? (long)period.Value.TotalMilliseconds : Timeout.Infinite);
  80. }
  81. return timer.Change(dueTime.Value, period.Value);
  82. }
  83. protected abstract void TimerCallback(object state);
  84. }
  85. internal class WayneTimer : WayneTimerChanger, ITimer
  86. {
  87. public WayneTimer(int id, IIdentifiableEntity parentEntity, string name)
  88. : base(id, parentEntity, name, null)
  89. {
  90. }
  91. public event EventHandler OnTimeout;
  92. protected override void TimerCallback(object state)
  93. {
  94. OnTimeout.Fire(this, EventArgs.Empty);
  95. }
  96. }
  97. internal class WayneTimer<TState> : WayneTimerChanger, ITimer<TState>
  98. {
  99. public WayneTimer(int id, IIdentifiableEntity parentEntity, string name, TState state)
  100. : base(id, parentEntity, name, state)
  101. {
  102. }
  103. public event EventHandler<EventArgs<TState>> OnTimeout;
  104. protected override void TimerCallback(object state)
  105. {
  106. OnTimeout.Fire(this, new EventArgs<TState>((TState)state));
  107. }
  108. }
  109. }