问题
I need help. I am working with an arraylist, and suddenly I get this error.
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);
}
}
Where
public class PC_list {
public string PcName;
public string ip;
public string status;
public string NickName;
public DateTime time;
}
Notes:
- onlinelist is an arraylist
- nowtime and x.time are DateTime.
Call Stack
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]
回答1:
foreach (PC_list x in onlinelist)
{
onlinelist.Remove(x); // cannot do this
}
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.
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>
.
And since your question is tagged multithreading
, it would also be a smart idea to consult the collections built with concurrency in mind.
回答2:
You cannot modify onlinelist in a foreach block. This is why you get this error. Try this:
ArrayList RemoveList = new ArrayList();
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);
}));
RemoveList.Add(x);
//Thread.Sleep(500);
}
}
foreach (PC_list x in RemoveList)
{
onlinelist.Remove(x);
}
回答3:
Yeah, your logic is broken. You're asking the iterator to move to the next object after x
in the list, but you've removed x
from the list, so there is no such thing. There are a lot of workarounds, but the traditional way to do it is to remove the previous object at the top of the loop. (This only works for collections that do not reorganize on removals.)
来源:https://stackoverflow.com/questions/7761959/collection-was-modified-enumeration-operation-may-not-execute-c-sharp