Remove an item from an arraylist inside foreach

T

tshad

I have an ArrayList of thread pointers that I want delete from my list when
the Thread in not alive and status is stopped. But I want to do it from the
foreach loop I am looping through. threadList is the ArrayList.

foreach (Thread theThread in threadList)
{
if ((!theThread.IsAlive) && (theThread.ThreadState.ToString ==
"Stopped"))
sw.WriteLine(" Status for Thread: {0} is {1} IsAlive: {2}",
theThread.Name, theThread.ThreadState,theThread.IsAlive);

// this is where I want to take the pointer out of the list - something
like threadList.current.remove.
}

Thanks,

Tom
 
N

Nicholas Paldino [.NET/C# MVP]

Tom,

This fails because you can not change the list while iterating through
the list with foreach (i.e. accessing through IEnumerable).

To get around this, you should use a regular for construct, starting
with the last index in the list, and then cycling through and removing them
if appropriate (you start at the last item in the list because if you remove
one while moving forward, you have to readjust your index to account for the
shifting of the indexes of the remaining elements).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You cannot. A collection cannot be modified while being enumerated.

If the order in comparing is not important (or you can assure that the
collection is lineal) you can do as Paldino said otherwise you have to keep
track of the items to be removed in a separated collection and later on
iterate on it removing the items from the original collection.
 
R

Rene

You cannot. A collection cannot be modified while being enumerated.

Hi Ignacio,

Well, I am not sure if I would go as far as saying that. I may be wrong but
I think someone could create their own collection (implementing their own
IEnumerator) and on their implementation they don't have to throw an error
if the collection is modified while enumerating.

Now, I am not saying if that is a good idea to do or not, all I am saying is
that just because all .Net collection will throw an error if the collection
is modified while enumerating does not mean that *all* collection will do
the same thing.

I don't believe this behavior governed by the runtime. In other words,
people should not count on this behavior by default... but then again, I may
be wrong!!


Cheers.
 
T

tshad

Nicholas Paldino said:
Tom,

This fails because you can not change the list while iterating through
the list with foreach (i.e. accessing through IEnumerable).

To get around this, you should use a regular for construct, starting
with the last index in the list, and then cycling through and removing
them if appropriate (you start at the last item in the list because if you
remove one while moving forward, you have to readjust your index to
account for the shifting of the indexes of the remaining elements).


That's as good a way as any.

Thanks,

Tom
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
I have an ArrayList of thread pointers that I want delete from my list
when the Thread in not alive and status is stopped. But I want to do it
from the foreach loop I am looping through. threadList is the ArrayList.

foreach (Thread theThread in threadList)
{
if ((!theThread.IsAlive) && (theThread.ThreadState.ToString ==
"Stopped"))
sw.WriteLine(" Status for Thread: {0} is {1} IsAlive: {2}",
theThread.Name, theThread.ThreadState,theThread.IsAlive);

// this is where I want to take the pointer out of the list -
something like threadList.current.remove.
}

Thanks,

Tom
 
M

Marc Gravell

Aside; if this was List<Thread> instead of ArrayList, you could do
something like below (using C# 3 lambda syntax, but an anonymous
method would work just as well):

threadList.RemoveAll(thread => {
if ((!thread.IsAlive) &&
(thread.ThreadState.ToString() == "Stopped")) {
sw.WriteLine("...foo...");
return true;
}
return false;
});

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top