System.InvalidOperationException: Collection was modified

前端 未结 6 1261
花落未央
花落未央 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.

提交回复
热议问题