From e03b0dc1bb6076c5f3a8ff61cda3739a79186495 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Mon, 8 Feb 2021 15:06:05 -0700 Subject: [PATCH] #600 Adds additional properties and constructors to GenericQueue --- .../Queues/GenericQueue.cs | 111 ++++++++++++++---- 1 file changed, 87 insertions(+), 24 deletions(-) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs index b4c6befa..46deddf7 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs @@ -18,42 +18,52 @@ namespace PepperDash_Essentials_Core.Queues private bool _delayEnabled; private int _delayTime; + private const Thread.eThreadPriority _defaultPriority = Thread.eThreadPriority.MediumPriority; + /// /// If the instance has been disposed. /// public bool Disposed { get; private set; } + /// + /// Returns the capacity of the CrestronQueue (fixed Size property) + /// + public int QueueCapacity + { + get + { + return _queue.Size; + } + } + + /// + /// Returns the number of elements currently in the CrestronQueue + /// + public int QueueCount + { + get + { + return _queue.Count; + } + } + /// /// Constructor with no thread priority /// /// public GenericQueue(string key) - : this(key, Thread.eThreadPriority.MediumPriority) + : this(key, _defaultPriority, 0, 0) { - } /// - /// Constructor for generic queue with no pacing + /// Constructor with queue size /// - /// Key - /// - public GenericQueue(string key, Thread.eThreadPriority priority) + /// + /// Fixed size for the queue to hold + public GenericQueue(string key, int capacity) + : this(key, _defaultPriority, capacity, 0) { - _key = key; - _queue = new CrestronQueue(25); - _worker = new Thread(ProcessQueue, null, Thread.eThreadStartOptions.Running) - { - Priority = priority - }; - - CrestronEnvironment.ProgramStatusEventHandler += programEvent => - { - if (programEvent != eProgramStatusEventType.Stopping) - return; - - Dispose(); - }; } /// @@ -61,12 +71,13 @@ namespace PepperDash_Essentials_Core.Queues /// /// Key /// Pacing in ms between actions - public GenericQueue(string key, int pacing) - : this(key) + public GenericQueue(int pacing, string key) + : this(key, _defaultPriority, 0, pacing) { - SetDelayValues(pacing); } + + /// /// Constructor with pacing and priority /// @@ -74,8 +85,52 @@ namespace PepperDash_Essentials_Core.Queues /// /// public GenericQueue(string key, int pacing, Thread.eThreadPriority priority) - : this(key, priority) + : this(key, priority, 0, pacing) { + } + + /// + /// Constructor with pacing, priority and capacity + /// + /// + /// + /// + public GenericQueue(string key, Thread.eThreadPriority priority, int capacity) + : this(key, priority, capacity, 0) + { + } + + /// + /// Constructor with pacing, priority and capacity + /// + /// + /// + /// + /// + public GenericQueue(string key, int pacing, Thread.eThreadPriority priority, int capacity) + : this(key, priority, capacity, pacing) + { + } + + /// + /// Constructor for generic queue with no pacing + /// + /// Key + /// + private GenericQueue(string key, Thread.eThreadPriority priority, int capacity, int pacing) + { + _key = key; + int cap = 25; // sets default + if (capacity > 0) + { + cap = capacity; // overrides default + } + _queue = new CrestronQueue(cap); + _worker = new Thread(ProcessQueue, null, Thread.eThreadStartOptions.Running) + { + Priority = priority + }; + SetDelayValues(pacing); } @@ -83,6 +138,14 @@ namespace PepperDash_Essentials_Core.Queues { _delayEnabled = pacing > 0; _delayTime = pacing; + + CrestronEnvironment.ProgramStatusEventHandler += programEvent => + { + if (programEvent != eProgramStatusEventType.Stopping) + return; + + Dispose(); + }; } ///