diff --git a/SmartThreadPool/SmartThreadPool.cs b/SmartThreadPool/SmartThreadPool.cs index dc067a5..4ca9591 100644 --- a/SmartThreadPool/SmartThreadPool.cs +++ b/SmartThreadPool/SmartThreadPool.cs @@ -300,6 +300,11 @@ public partial class SmartThreadPool : WorkItemsGroupBase, IDisposable /// private bool _isDisposed; + /// + /// Indicate whether the SmartThreadPool is idle + /// + private bool _isIdle; + /// /// Holds all the WorkItemsGroup instaces that have at least one /// work item int the SmartThreadPool @@ -338,6 +343,11 @@ public partial class SmartThreadPool : WorkItemsGroupBase, IDisposable /// private event ThreadTerminationHandler _onThreadTermination; + /// + /// The OnIdle event + /// + private event WorkItemsGroupIdleHandler _onIdle; + #endregion #region Per thread properties @@ -1589,6 +1599,20 @@ public override int WaitingCallbacks } } + public new bool IsIdle + { + get => _isIdle; + protected set + { + // Fire only when we're setting the state to idle and if it wasn't idle already + if (value && !_isIdle) + { + _onIdle?.Invoke(this); + } + _isIdle = value; + } + } + /// /// Get an array with all the state objects of the currently running items. /// The array represents a snap shot and impact performance. @@ -1670,21 +1694,11 @@ public override bool WaitForIdle(int millisecondsTimeout) /// /// This event is fired when all work items are completed. /// (When IsIdle changes to true) - /// This event only work on WorkItemsGroup. On SmartThreadPool - /// it throws the NotImplementedException. /// public override event WorkItemsGroupIdleHandler OnIdle { - add - { - throw new NotImplementedException("This event is not implemented in the SmartThreadPool class. Please create a WorkItemsGroup in order to use this feature."); - //_onIdle += value; - } - remove - { - throw new NotImplementedException("This event is not implemented in the SmartThreadPool class. Please create a WorkItemsGroup in order to use this feature."); - //_onIdle -= value; - } + add => _onIdle += value; + remove => _onIdle -= value; } internal override void PreQueueWorkItem()