/// <summary> /// Unschedule selected items for processing and apply an action to each unscheduled item. /// </summary> /// <param name="itemSelector">A function to test each element for removal.</param> /// <param name="processUnscheduled">An optional action to execute on each unscheduled item.</param> public void Unschedule(Func <T, bool> itemSelector, Action <T> processUnscheduled = null) { if (itemSelector == null) { throw new ArgumentNullException(nameof(itemSelector)); } T[] unscheduledItems; lock (queueLock) { var keptItems = processingQueue.Where(x => !itemSelector(x)).ToArray(); unscheduledItems = processingQueue.Where(itemSelector).ToArray(); processingQueue.Clear(); queueSet.Clear(); foreach (var item in keptItems) { processingQueue.Enqueue(item); queueSet.Add(item); } } if (processUnscheduled != null) { foreach (var item in unscheduledItems) { processUnscheduled(item); } } if (unscheduledItems.Length > 0) { Unscheduled?.Invoke(this, BuildEventArgs()); } }
private void OnUnscheduled() { Unscheduled?.Invoke(this, EventArgs.Empty); }