469,658 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

Remove an item from an arraylist inside foreach

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
Jan 31 '08 #1
4 17493
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tshad" <ts***@dslextreme.comwrote in message
news:eX**************@TK2MSFTNGP04.phx.gbl...
>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

Jan 31 '08 #2
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.
Jan 31 '08 #3
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:47**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"tshad" <ts***@dslextreme.comwrote in message
news:eX**************@TK2MSFTNGP04.phx.gbl...
>>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


Feb 1 '08 #4
Aside; if this was List<Threadinstead 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
Feb 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by K. Byanjankar | last post: by
2 posts views Thread by MFRASER | last post: by
2 posts views Thread by James | last post: by
4 posts views Thread by Jon Paal | last post: by
10 posts views Thread by =?Utf-8?B?YmJn?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.