By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Remove an item from an arraylist inside foreach

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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.