简体   繁体   English

System.InvalidOperationException:集合已修改; 枚举操作可能无法执行

[英]System.InvalidOperationException: Collection was modified; enumeration operation may not execute

I'm having a problem when getting the tempkey which is the source loaded from another function.我在获取从另一个 function 加载的源的 tempkey 时遇到问题。 Normally the source contains 18 to 20 keys randomly.通常,源随机包含 18 到 20 个密钥。 This problem happens once in awhile.这个问题偶尔会发生一次。 How to access the source keys without getting exception error like " Collection was modified; enumeration operation may not execute" .如何访问源键而不会出现“集合已修改;枚举操作可能无法执行”之类的异常错误。

private static IDictionary<string, BitArray> source;

public bool Check()
{
    string tempKey = source.Keys.FirstOrDefault(k => k.EndsWith(key));   
}

One option is to use ConcurrentDictionary or ImmutableDictionary .一种选择是使用ConcurrentDictionaryImmutableDictionary

ConcurrentDictionary 's Keys accessor returns a copy of the keys collection which you can than inspect even if other code is modifying the original dictionary. ConcurrentDictionaryKeys访问器返回密钥集合的副本,即使其他代码正在修改原始字典,您也可以检查该副本。


Note 1. Dictionary 's doco (the not Concurrent flavour) explicitly talks about thread-safety and the situation you encounered注 1. Dictionary的 doco (非 Concurrent 风格)明确谈论线程安全和您遇到的情况

(...) enumerating through a collection is intrinsically not a thread-safe procedure. (...) 通过集合枚举本质上不是线程安全的过程。 In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration.在枚举与写访问竞争的极少数情况下,必须在整个枚举期间锁定集合。

I recommend reading the whole short section on Dictionary's Thread Safety .我建议阅读 Dictionary's Thread Safety的整个简短部分。


Note 2. You can be tempted to ToList() source.Keys which will improve the situation (the exception will seem to go away, mostly), but it won't fix the issue for good.注意 2. 您可以尝试使用ToList() source.Keys来改善这种情况(大多数情况下,该异常似乎与 go 无关),但它不会永久解决问题。

This exception occur when your code is modifying the collection and within same code block or thread you are also trying to iterate the collection or put where condition on it.当您的代码正在修改集合并且在同一代码块或线程中您还尝试迭代集合或在其上放置 where 条件时,会发生此异常。

Best way to avoid is to do the modifications on the collection first.最好的避免方法是先对集合进行修改。 And after doing all the modification(add/remove etc) then you check the collection.在完成所有修改(添加/删除等)之后,您检查集合。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidOperationException: &#39;集合已修改; 枚举操作可能无法执行。 - System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.' System.invalidoperationexception:集合被修改; 枚举操作可能无法在 .net 5 api 项目中执行 - System.invalidoperationexception: collection was modified; enumeration operation may not execute in .net 5 api project 我收到`System.InvalidOperationException:收集被修改; 枚举操作可能无法执行,莫名其妙 - I'm getting `System.InvalidOperationException: Collection was modified; enumeration operation may not execute`, inexplicably InvalidOperationException:集合已修改; 枚举操作可能无法执行 - InvalidOperationException: Collection was modified; enumeration operation may not execute Wix和FinalBuilder-InvalidOperationException:集合已修改; 枚举操作可能无法执行 - Wix and FinalBuilder - InvalidOperationException: Collection was modified; enumeration operation may not execute 识别 InvalidOperationException “集合已修改; 枚举操作可能无法执行。” - Identify InvalidOperationException “Collection was modified; enumeration operation may not execute.” 集合已修改; 枚举操作可能无法执行 - Collection was modified; enumeration operation may not execute 集合已修改; 枚举操作可能无法执行-为什么? - Collection was modified; enumeration operation may not execute - why? 收集被修改; 枚举操作可能无法执行 - Collection was modified; enumeration operation may not execute - 收藏被修改; 枚举操作可能不会执行 - -Collection was modified; enumeration operation may not execute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM