收藏集已修改;枚举操作可能无法执行. C# [英] Collection was modified; enumeration operation may not execute. C#

查看:448
本文介绍了收藏集已修改;枚举操作可能无法执行. C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助.我正在使用一个数组列表,突然出现此错误.

I need help. I am working with an arraylist, and suddenly I get this error.

mscorlib.dll中发生了'System.InvalidOperationException'类型的未处理异常

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

其他信息:集合已修改;枚举操作可能无法执行.

Additional information: Collection was modified; enumeration operation may not execute.

这是显示异常的代码...

This is the code where it shows the exception...

foreach (PC_list x in onlinelist) {
  if ((nowtime.Subtract(x.time)).TotalSeconds > 5) {
    Invoke(new MethodInvoker(delegate {
      index = Main_ListBox.FindString(x.PcName);
      if(index != ListBox.NoMatches)
      Main_ListBox.Items.RemoveAt(index);
    }));
    onlinelist.Remove(x);
    //Thread.Sleep(500);
  }
}

哪里

public class PC_list {
    public string PcName;
    public string ip;
    public string status;
    public string NickName;
    public DateTime time;

}

注释:

  • onlinelist是一个数组列表
  • nowtime和x.time是DateTime.

调用堆栈

mscorlib.dll!System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() + 0x122 bytes    
BlueBall.exe!BlueBall.BlueBall.clean_arraylist() Line 74 + 0x1a8 bytes  C#
BlueBall.exe!BlueBall.BlueBall.server() Line 61 + 0x8 bytes C#
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
[Native to Managed Transition]

推荐答案

foreach (PC_list x in onlinelist)
{            
     onlinelist.Remove(x); // cannot do this
}

这是问题的核心.在foreach中对其进行迭代时,无法从集合中删除该项目.您的选择是在循环之前对列表进行本地的副本,循环遍历副本,然后从原始副本中删除.或者,您可以保留一个单独的项目列表,以在完成原始循环后在 之后将其删除.或者,您可以切换到for循环并向后循环遍历,这使您可以随时从 end 中删除项目.

This is the heart of the problem. You cannot remove an item from a collection as you are iterating over it in a foreach. Your options are to make a local copy of the list prior to the loop, loop over the copy, and remove from the original. Or you can keep a separate list of items to remove after you finish the original loop. Or you can switch to a for loop and iterate over it backwards, which allows you to remove items from the end as you go.

在这里,如果您不习惯使用C#1/.NET 1.1/Visual Studio 2003,则可能要考虑从ArrayList切换到更强的List<T>,其中T是集合中对象的类型.在您的情况下,这将是List<PC_list>.您可以在System.Collections.Generic.List<T>上找到它.

While you're here, if you are not stuck working with C# 1 / .NET 1.1 / Visual Studio 2003, you might want to consider switching from ArrayList to the stronger List<T>, where T is the type of the object in the collection. In your case, that would be a List<PC_list>. You can find it at System.Collections.Generic.List<T>.

并且由于您的问题被标记为multithreading,因此咨询

And since your question is tagged multithreading, it would also be a smart idea to consult the collections built with concurrency in mind.

这篇关于收藏集已修改;枚举操作可能无法执行. C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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