EventQueue.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. namespace Universal;
  2. public class EventQueue<T> : IDisposable
  3. {
  4. public EventQueue(Action<T> queueHandler, int timeoutMillionSecond = Timeout.Infinite, Action<T>? timeoutHandler = null)
  5. {
  6. if (timeoutMillionSecond < -1)
  7. throw new ArgumentException($"Invalid timeout time {timeoutMillionSecond}");
  8. this._queueHandler = queueHandler;
  9. this._timeoutHandler = timeoutHandler;
  10. this._timeoutMillionSeconds = timeoutMillionSecond;
  11. Task.Factory.StartNew(WorkingTask, TaskCreationOptions.LongRunning);
  12. }
  13. private readonly ConcurrentQueue<T> _queue = [];
  14. private readonly Action<T>? _queueHandler;
  15. private CancellationToken _cancellation = new(false);
  16. private readonly AutoResetEvent _queueEvent = new(false);
  17. private readonly ManualResetEvent _pauseController = new(true);
  18. private Timer? _timeoutTimer;
  19. private bool disposedValue;
  20. private readonly int _timeoutMillionSeconds;
  21. private readonly Action<T>? _timeoutHandler;
  22. public int Count { get => this._queue.Count; }
  23. public void Pause() => this._pauseController.Reset();
  24. public void Resume() => this._pauseController.Set();
  25. public bool Enqueue(T input)
  26. {
  27. if (this._queue is null)
  28. return false;
  29. this._queue.Enqueue(input);
  30. this._queueEvent.Set();
  31. return true;
  32. }
  33. private void WorkingTask()
  34. {
  35. for (T? content; !this._cancellation.IsCancellationRequested; this._pauseController.WaitOne(-1))
  36. {
  37. if (!this._queue.TryDequeue(out content))
  38. {
  39. this._queueEvent.Reset();
  40. this._queueEvent.WaitOne(-1);
  41. continue;
  42. }
  43. if (!this._timeoutMillionSeconds.Equals(Timeout.Infinite))
  44. this._timeoutTimer = new(TimeoutCallBack, content, this._timeoutMillionSeconds, int.MaxValue);
  45. try
  46. {
  47. this._queueHandler?.Invoke(content);
  48. }
  49. catch
  50. {
  51. }
  52. this._timeoutTimer?.Dispose();
  53. }
  54. this._queue.Clear();
  55. }
  56. private void TimeoutCallBack(object? state)
  57. {
  58. this._timeoutTimer?.Dispose();
  59. if (state is not T content)
  60. return;
  61. this._timeoutHandler?.Invoke(content);
  62. }
  63. #region Dispose
  64. protected virtual void Dispose(bool disposing)
  65. {
  66. if (!disposedValue)
  67. {
  68. if (disposing)
  69. {
  70. // TODO: dispose managed state (managed objects)
  71. this._cancellation = new(true);
  72. }
  73. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  74. // TODO: set large fields to null
  75. disposedValue = true;
  76. }
  77. }
  78. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  79. ~EventQueue()
  80. {
  81. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  82. Dispose(disposing: false);
  83. }
  84. public void Dispose()
  85. {
  86. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  87. Dispose(disposing: true);
  88. GC.SuppressFinalize(this);
  89. }
  90. #endregion
  91. }
  92. public class PriorityEventQueue<T> : IDisposable
  93. {
  94. public PriorityEventQueue(Action<T> queueHandler, int timeoutMillionSecond = Timeout.Infinite, Action<T>? timeoutHandler = null)
  95. {
  96. if (timeoutMillionSecond < -1)
  97. throw new ArgumentException($"Invalid timeout time {timeoutMillionSecond}");
  98. this._queueHandler = queueHandler;
  99. this._timeoutHandler = timeoutHandler;
  100. this._timeoutMillionSeconds = timeoutMillionSecond;
  101. Task.Factory.StartNew(WorkingTask, TaskCreationOptions.LongRunning);
  102. }
  103. private readonly PriorityQueue<T, int> _queue = new();
  104. private readonly Action<T>? _queueHandler;
  105. private CancellationToken _cancellation = new(false);
  106. private readonly AutoResetEvent _queueEvent = new(false);
  107. private readonly ManualResetEvent _pauseController = new(true);
  108. private Timer? _timeoutTimer;
  109. private bool disposedValue;
  110. private readonly int _timeoutMillionSeconds;
  111. private readonly Action<T>? _timeoutHandler;
  112. public int Count { get => this._queue.Count; }
  113. public void Pause() => this._pauseController.Reset();
  114. public void Resume() => this._pauseController.Set();
  115. public bool Enqueue(T input, int priority)
  116. {
  117. if (this._queue is null)
  118. return false;
  119. this._queue.Enqueue(input, priority);
  120. this._queueEvent.Set();
  121. return true;
  122. }
  123. private void WorkingTask()
  124. {
  125. for (T? content; !this._cancellation.IsCancellationRequested; this._pauseController.WaitOne(-1))
  126. {
  127. try
  128. {
  129. content = this._queue.Dequeue();
  130. }
  131. catch
  132. {
  133. this._queueEvent.Reset();
  134. this._queueEvent.WaitOne(-1);
  135. continue;
  136. }
  137. if (!this._timeoutMillionSeconds.Equals(Timeout.Infinite))
  138. this._timeoutTimer = new(TimeoutCallBack, content, this._timeoutMillionSeconds, int.MaxValue);
  139. try
  140. {
  141. this._queueHandler?.Invoke(content);
  142. }
  143. catch
  144. {
  145. }
  146. this._timeoutTimer?.Dispose();
  147. }
  148. this._queue.Clear();
  149. }
  150. private void TimeoutCallBack(object? state)
  151. {
  152. this._timeoutTimer?.Dispose();
  153. if (state is not T content)
  154. return;
  155. this._timeoutHandler?.Invoke(content);
  156. }
  157. #region Dispose
  158. protected virtual void Dispose(bool disposing)
  159. {
  160. if (!disposedValue)
  161. {
  162. if (disposing)
  163. {
  164. // TODO: dispose managed state (managed objects)
  165. this._cancellation = new(true);
  166. }
  167. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  168. // TODO: set large fields to null
  169. disposedValue = true;
  170. }
  171. }
  172. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  173. ~PriorityEventQueue()
  174. {
  175. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  176. Dispose(disposing: false);
  177. }
  178. public void Dispose()
  179. {
  180. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  181. Dispose(disposing: true);
  182. GC.SuppressFinalize(this);
  183. }
  184. #endregion
  185. }