foreach循环 - 集合被修改的异常 [英] foreach loop - collection was modified exception

查看:258
本文介绍了foreach循环 - 集合被修改的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误:


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

在我的代码中,有不断需要读取的字符,对象等的散列表除去。我的代码使用 foreach 循环来处理哈希表中的所有值,但是说一个字符以 foreach 循环...哈希表被修改...繁荣,错误。

请注意任何 foreach 代码被复制到列表中,并从那里编辑父列表,这不是问题。问题是读取经常编辑的散列表。



所以我明白问题是什么,只是不是最好的方法关于补救。下面是一个简单的代码示例,它偶尔会抛出一个集合修改后的错误:

$ foreach(在World.H_Chars.Values中的字符CC)$ (CC.MyGuild!= null& CC.MyGuild.GuildName!= G.GuildName
&&& CC.MyGuild.Allies.ContainsValue(G.GuildName)b $ b {

{
CC.MyClient.AddSend(Data); //错误在这行
CC.MyClient.EndSend();






$ b所以我想问一下,最好的方法是什么通过一个不断被修改的对象的哈希表?

解决方案

您可以尝试使用 System.Collections.Concurrent.ConcurrentDictionary< TKey,TValue> 。这是一个关键:值对数据结构是线程安全的(例如,它是一个通用的HashTable类型实现 - 它具有元素的O(1)查找)。枚举时可以更新。但是,您需要注意的是,当枚举这种类型的集合时,您可能会得到脏的读取(即,您正在迭代的集合是迭代开始时字典的快照 - 所以发生的更新在开始迭代之后,在集合的当前迭代中可能不可见)。

还有一个 System.Collections.Concurrent。 ConcurrentBag< T> ,它是 System.Collections.Generic.List< T> 的线程安全的等价物。 b

I'm encountering the error:

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

Throughout my code there are hash tables of characters, objects, etc. that constantly need to be read and removed. My code uses a foreach loop to do something with all of the values inside the hash table, but say one character logs off as the foreach is looping... and the hash table is modified... boom, error.

Note that any foreach code that directly modifies the hash table are copied into lists and the parent list is edited from there, that is not the issue. The issue is the reading of hash tables that are constantly edited.

So I understand what the problem is, just not the best way to go about remedying it. Here's a simple example of code that occasionally throws a collection modified error:

foreach (Character CC in World.H_Chars.Values)
{
   if (CC.MyGuild != null && CC.MyGuild.GuildName != G.GuildName
      && CC.MyGuild.Allies.ContainsValue(G.GuildName))
   {
      CC.MyClient.AddSend(Data); // error on this line
      CC.MyClient.EndSend();
   }
}

So I guess my question is, what's the best way to go through a hash table of objects that is constantly being modified?

解决方案

You could try using a System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>. This as a key:value pair data structure that is thread-safe (e.g. it's a generic HashTable type implementation—it has O(1) lookup of elements). It can be updated while enumerating. However, you do need to be aware that when enumerating over this type of collection you are possibly getting "dirty" reads (i.e. the collection you're iterating over is a snapshot of the dictionary at the time the iteration began—so updates that occur after you've began iterating may not be visible in the current iteration of the collection).

There is also a System.Collections.Concurrent.ConcurrentBag<T> that is the thread-safe equivalent of System.Collections.Generic.List<T>.

这篇关于foreach循环 - 集合被修改的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆