FixedSizedQueue.cs 636 B

1234567891011121314151617181920212223242526272829
  1. using System.Collections.Concurrent;
  2. namespace Server.Utils
  3. {
  4. public class FixedSizedQueue<T> : ConcurrentQueue<T>
  5. {
  6. private readonly object syncObject = new object();
  7. public int Size { get; private set; }
  8. public FixedSizedQueue(int size)
  9. {
  10. Size = size;
  11. }
  12. public new void Enqueue(T obj)
  13. {
  14. base.Enqueue(obj);
  15. lock (syncObject)
  16. {
  17. while (base.Count > Size)
  18. {
  19. T outObj;
  20. base.TryDequeue(out outObj);
  21. }
  22. }
  23. }
  24. }
  25. }