代码之家  ›  专栏  ›  技术社区  ›  Bastien Vandamme

控制队列(或列表)的容量

  •  0
  • Bastien Vandamme  · 技术社区  · 4 年前

    我有课

        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>();
        }
    

    让我解释一下,Task对象还包含一个变量(可能是另一个Timespan),该变量包含执行任务的时间。

    当我在任务列表中添加新任务时,如果达到了我的容量,是否可以在我的ProductionQueue中添加事件、观察者或任何其他行为来引发异常?

    0 回复  |  直到 4 年前
        1
  •  0
  •   XouDo Sagar Tambade    4 年前

    我已经根据您的初始代码编写了这个简单的解决方案,但使用 ObservableCollection ,因此每次修改集合时都可以检查容量。

    可以使用 NotifyCollectionChangedEventArgs (修改类型、NewItems、OldItems等),只重新计算更改的内容,而不是每次迭代整个集合。

       // 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");           
           }
       }
    }
    
    

    下面是一个使用它的程序示例:

    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);
        }
    }
    

    控制台输出:

    collection changed
    collection changed
    Exception occured: queue time capacity exceeded