System.InvalidOperationException: Collection was modified

前端 未结 6 1139
花落未央
花落未央 2020-11-30 15:05

I am getting a following exception while enumerating through a queue:

System.InvalidOperationException: Collection was modified; enumeration opera

相关标签:
6条回答
  • 2020-11-30 15:29

    I think all you need to do is stop using the foreach and instead switch it over to a for loop

    for(int i = 0; i < tpotActionQueue.Length; i++)
    {
         TpotAction action = tpotActionQueue[i];
    
         if (action is WriteChannel)
         {
            channelWrites.Add((WriteChannel)action);
            lock(tpotActionQueue)
            {
               action.Status = RecordStatus.Batched;
            }
         }
    }
    

    Regards, Mike.

    0 讨论(0)
  • 2020-11-30 15:34

    How about some LINQy goodness?

    private bool extractWriteActions(out List<WriteChannel> channelWrites)
    {
    
       channelWrites= tpotActionQueue.Where<WriteChannel>(x => x is WriteChannel).ToList()
    
       foreach(WriteChannel channel in channelWrites) {
          channel.Status = RecordStatus.Batched;
       }
    
      return ( channelWrites.Count > 0);
    }
    
    0 讨论(0)
  • 2020-11-30 15:36

    You are allowed to change the value in an item in a collection. The error you're getting means that an item was either added or removed i.e.: the collection itself was modified, not an item inside the collection. This is most likely caused by another thread adding or removing items to this collection.

    You should lock your queue at the beginning of your method, to prevent other Threads modifying the collection while you are accessing it. Or you could lock the collection before even calling this method.

    private bool extractWriteActions(out List<WriteChannel> channelWrites)
        {
          lock(tpotActionQueue)
          {
            channelWrites = new List<WriteChannel>();
            foreach (TpotAction action in tpotActionQueue)
            {
                if (action is WriteChannel)
                {
                    channelWrites.Add((WriteChannel)action);
    
                      action.Status = RecordStatus.Batched;
    
               }
            }
          }
           return (channelWrites.Count > 0);
       }
    
    0 讨论(0)
  • 2020-11-30 15:52

    I think I had a similar exception when using a foreach loop on a Collection where I tried to remove items from the Collection (or it may have been a List, I can't remember). I ended up getting around it by using a for loop. Perhaps try something like the following:

    for (int i=0; i<tpotActionQueue.Count(); i++)
    {
        TpotAction action = tpotActionQueue.Dequeue();
        if (action is WriteChannel)
        {
            channelWrites.Add((WriteChannel)action);
            lock(tpotActionQueue)
            {
                action.Status = RecordStatus.Batched;
            }
        }
    }
    
    0 讨论(0)
  • You don't have a definition for tpotActionQueue, but if it's just a normal List<TpotAction> then that line is not your problem. Modifying the collection is adding or removing members - not setting a property on a contained object.

    You have a lock(tpotActionQueue) and a tag of thread-safety, so my guess is there's another thread adding or removing items from tpotActionQueue while you're enumerating. You probably need to synchronize those accesses.

    0 讨论(0)
  • 2020-11-30 15:55

    I think you must have some other thread modifying the tpotActionQueue while you're iterating over it. Since you're only locking that queue inside the for loop this is possible.

    0 讨论(0)
提交回复
热议问题