Enumerators that need to be changed at runtime, avoiding exception

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Hi guys

In regards to the foreach and for loops. I am trying to avoid this
exception:exception:

"Collection was modified; enumeration operation may not execute.
System.InvalidOperationException"

The problem is i have a timer, that checks at an interval for any changes to
a list if there are changes it removes the corresponding item:

See below:

public override bool RemoveRoom(int roomId)
{
try
{
for (int i = 0; i < _roomList.Count; i++)
{
Room room = (Room)_roomList;

if (room.RoomId.Equals(roomId))
{
//clear timers
_timeManager.RemoveAll(roomId);

_roomList.RemoveAt(i);

return true;
}
}

return false;
}
catch(Exception e)
{
Console.WriteLine("Error removing room, exception: " +
e.Message + " " + e.GetType().ToString());
return false;
}
}

I understand why i am getting the error, i am changing the enumeratoed list
by removing an item durin the enumeration. So my question is, how do i do
this saftely?

Another issue i have is a render loop that runs in a for each liek this:

foreach(Object o in objList)
{
o.RenderObject();
}

And that runs all the time, but objects need to be removed from that list
when they are no longer 'alive'. Again how do i do that saftely?

Is the answer to lock it so that no other thread accesses it while the
object is removed? How would this hit efficiency is this even the right way
about it?

So to summarise, how do you remove an item from a list/array while it is
being looked through via a loop safetly? Thanks is advance
 
WOW thanks!

John Duval said:
Hi Daniel,
You might want to check out Eric Gunnerson's IterIsolate:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/Csharp01212002.asp

Hope that helps,
John
Hi guys

In regards to the foreach and for loops. I am trying to avoid this
exception:exception:

"Collection was modified; enumeration operation may not execute.
System.InvalidOperationException"

The problem is i have a timer, that checks at an interval for any changes
to
a list if there are changes it removes the corresponding item:

See below:

public override bool RemoveRoom(int roomId)
{
try
{
for (int i = 0; i < _roomList.Count; i++)
{
Room room = (Room)_roomList;

if (room.RoomId.Equals(roomId))
{
//clear timers
_timeManager.RemoveAll(roomId);

_roomList.RemoveAt(i);

return true;
}
}

return false;
}
catch(Exception e)
{
Console.WriteLine("Error removing room, exception: " +
e.Message + " " + e.GetType().ToString());
return false;
}
}

I understand why i am getting the error, i am changing the enumeratoed
list
by removing an item durin the enumeration. So my question is, how do i do
this saftely?

Another issue i have is a render loop that runs in a for each liek this:

foreach(Object o in objList)
{
o.RenderObject();
}

And that runs all the time, but objects need to be removed from that list
when they are no longer 'alive'. Again how do i do that saftely?

Is the answer to lock it so that no other thread accesses it while the
object is removed? How would this hit efficiency is this even the right
way
about it?

So to summarise, how do you remove an item from a list/array while it is
being looked through via a loop safetly? Thanks is advance

 
I could be interesting to get same method working with Lists so you could
remove current and do the index fixup internally.

--
William Stacey [C# MVP]

| Hi Daniel,
| You might want to check out Eric Gunnerson's IterIsolate:
|
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/Csharp01212002.asp
|
| Hope that helps,
| John
|
| Daniel wrote:
| > Hi guys
| >
| > In regards to the foreach and for loops. I am trying to avoid this
| > exception:exception:
| >
| > "Collection was modified; enumeration operation may not execute.
| > System.InvalidOperationException"
| >
| > The problem is i have a timer, that checks at an interval for any
changes to
| > a list if there are changes it removes the corresponding item:
| >
| > See below:
| >
| > public override bool RemoveRoom(int roomId)
| > {
| > try
| > {
| > for (int i = 0; i < _roomList.Count; i++)
| > {
| > Room room = (Room)_roomList;
| >
| > if (room.RoomId.Equals(roomId))
| > {
| > //clear timers
| > _timeManager.RemoveAll(roomId);
| >
| > _roomList.RemoveAt(i);
| >
| > return true;
| > }
| > }
| >
| > return false;
| > }
| > catch(Exception e)
| > {
| > Console.WriteLine("Error removing room, exception: " +
| > e.Message + " " + e.GetType().ToString());
| > return false;
| > }
| > }
| >
| > I understand why i am getting the error, i am changing the enumeratoed
list
| > by removing an item durin the enumeration. So my question is, how do i
do
| > this saftely?
| >
| > Another issue i have is a render loop that runs in a for each liek this:
| >
| > foreach(Object o in objList)
| > {
| > o.RenderObject();
| > }
| >
| > And that runs all the time, but objects need to be removed from that
list
| > when they are no longer 'alive'. Again how do i do that saftely?
| >
| > Is the answer to lock it so that no other thread accesses it while the
| > object is removed? How would this hit efficiency is this even the right
way
| > about it?
| >
| > So to summarise, how do you remove an item from a list/array while it is
| > being looked through via a loop safetly? Thanks is advance
|
 

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

Back
Top