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