EventQueue.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. namespace UniversalNetFrame451;
  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 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. this._queueHandler?.Invoke(content);
  48. this._timeoutTimer?.Dispose();
  49. }
  50. this._queue = null;
  51. }
  52. private void TimeoutCallBack(object state)
  53. {
  54. this._timeoutTimer?.Dispose();
  55. if (state is not T content)
  56. return;
  57. this._timeoutHandler?.Invoke(content);
  58. }
  59. #region Dispose
  60. protected virtual void Dispose(bool disposing)
  61. {
  62. if (!disposedValue)
  63. {
  64. if (disposing)
  65. {
  66. // TODO: dispose managed state (managed objects)
  67. this._cancellation = new(true);
  68. }
  69. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  70. // TODO: set large fields to null
  71. disposedValue = true;
  72. }
  73. }
  74. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  75. ~EventQueue()
  76. {
  77. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  78. Dispose(disposing: false);
  79. }
  80. public void Dispose()
  81. {
  82. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  83. Dispose(disposing: true);
  84. GC.SuppressFinalize(this);
  85. }
  86. #endregion
  87. }