EventQueue.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 T? LastItem { get; private set; }
  23. public int Count { get => this._queue.Count; }
  24. public void Pause() => this._pauseController.Reset();
  25. public void Resume() => this._pauseController.Set();
  26. public bool Enqueue(T input)
  27. {
  28. if (this._queue is null)
  29. return false;
  30. this.LastItem = input;
  31. this._queue.Enqueue(input);
  32. this._queueEvent.Set();
  33. return true;
  34. }
  35. private void WorkingTask()
  36. {
  37. for (T? content; !this._cancellation.IsCancellationRequested; this._pauseController.WaitOne(-1))
  38. {
  39. if (!this._queue.TryDequeue(out content))
  40. {
  41. this._queueEvent.Reset();
  42. this._queueEvent.WaitOne(-1);
  43. continue;
  44. }
  45. if (!this._timeoutMillionSeconds.Equals(Timeout.Infinite))
  46. this._timeoutTimer = new(TimeoutCallBack, content, this._timeoutMillionSeconds, int.MaxValue);
  47. try
  48. {
  49. this._queueHandler?.Invoke(content);
  50. }
  51. catch
  52. {
  53. }
  54. this._timeoutTimer?.Dispose();
  55. }
  56. this._queue.Clear();
  57. }
  58. private void TimeoutCallBack(object? state)
  59. {
  60. this._timeoutTimer?.Dispose();
  61. if (state is not T content)
  62. return;
  63. this._timeoutHandler?.Invoke(content);
  64. }
  65. #region Dispose
  66. protected virtual void Dispose(bool disposing)
  67. {
  68. if (!disposedValue)
  69. {
  70. if (disposing)
  71. {
  72. // TODO: dispose managed state (managed objects)
  73. this._cancellation = new(true);
  74. }
  75. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  76. // TODO: set large fields to null
  77. disposedValue = true;
  78. }
  79. }
  80. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  81. ~EventQueue()
  82. {
  83. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  84. Dispose(disposing: false);
  85. }
  86. public void Dispose()
  87. {
  88. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  89. Dispose(disposing: true);
  90. GC.SuppressFinalize(this);
  91. }
  92. #endregion
  93. }