'Control the capacity of a queue (or a list)
I have a class
public class ProductionQueue
{
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
public string? Name { get; set; }
/// <summary>
/// Maximum capacity of the queue. Keep it null for inifinte capacity.
/// </summary>
public TimeSpan? Capacity { get; set; }
/// <summary>
/// Gets or sets the tasks.
/// </summary>
/// <value>
/// The tasks.
/// </value>
public IList<Task> Tasks { get; set; } = new List<Task>();
}
Let me just explain that the Task object also contains a variable (probably another Timespan) that contains the time to perform the task.
Is it possible to add an event, an observer or any other behaviour in my ProductionQueue to throw an exception if my Capacity is reached when I add a new task in my Task list?
Solution 1:[1]
I have written this simple solution based on your initial code but with using an ObservableCollection, so the capacity can be check every time the collection is modified.
It could be done in more effecient way using the data provided by NotifyCollectionChangedEventArgs (type of modification, NewItems, OldItems etc.) to recompute only what's changed, instead of iterating through the whole collection every time.
// simple object for the sake of this example
public class Task
{
public TimeSpan TimeToPerform;
}
public class ProductionQueue
{
public string? Name { get; set; }
/// <summary>
/// Maximum capacity of the queue. Keep it null for inifinte capacity.
/// </summary>
public TimeSpan? Capacity { get; set;}
public ObservableCollection<Task> Tasks = new ObservableCollection <Task>();
// ProductionQueue ctor
public ProductionQueue(TimeSpan? initialCapacity)
{
this.Capacity = initialCapacity;
// subscribe to know when the collection gets changed
this.Tasks.CollectionChanged += (s, e) => {
Console.WriteLine("collection changed");
this.checkCapacity();
};
}
private void checkCapacity()
{
var totalTime = TimeSpan.Zero;
foreach (var task in this.Tasks)
{
totalTime+= task.TimeToPerform;
}
if (totalTime > this.Capacity)
throw new Exception("queue time capacity exceeded");
}
}
}
And here is an example of program using it:
public static void Main(string[] args)
{
try
{
var PQ = new ProductionQueue(TimeSpan.FromHours(1));
PQ.Tasks.Add(new Task(){ TimeToPerform=TimeSpan.FromMinutes(40)});
PQ.Tasks.Add(new Task(){ TimeToPerform=TimeSpan.FromMinutes(30)});
Console.WriteLine("Tasks added without problem");
}
catch(Exception e)
{
Console.WriteLine("Exception occured: "+e.Message);
}
}
Console output:
collection changed collection changed Exception occured: queue time capacity exceeded
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | XouDo |
