Removing items from list while performing operations on it
-
I would like to know what happens if objects are removed from a list while operations are performed on this list? Let's say we have this class:
class MyListObject
{
int MyInt {get;set;}
string MyString {get;set;}
}And we have a list of "MyListObject" called "MyList". We also have an eventhandler that can trigger randomly. In that eventhandler a random item in "MyList" is removed from "MyList". Now, what happens if I do something like:
MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});
And at the same time we perform this operation on the list the event is triggered and the eventhandler removes some item from the list. Will that throw some kind of exception? I don't know how to test it so that's why I'm asking you guys. If this can cause problems, how can I get around this? Thanks for help!
-
I would like to know what happens if objects are removed from a list while operations are performed on this list? Let's say we have this class:
class MyListObject
{
int MyInt {get;set;}
string MyString {get;set;}
}And we have a list of "MyListObject" called "MyList". We also have an eventhandler that can trigger randomly. In that eventhandler a random item in "MyList" is removed from "MyList". Now, what happens if I do something like:
MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});
And at the same time we perform this operation on the list the event is triggered and the eventhandler removes some item from the list. Will that throw some kind of exception? I don't know how to test it so that's why I'm asking you guys. If this can cause problems, how can I get around this? Thanks for help!
You get a
System.InvalidOperationException
exception with the message "Collection was modified; enumeration operation may not execute.". To fix:lock (MyList)
{
MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});
}This locks the object so that only the code block can modify / access it. If you put more than one lock around the object in different places, you need to think about race and deadlock conditions, but for your example it should be fine.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
I would like to know what happens if objects are removed from a list while operations are performed on this list? Let's say we have this class:
class MyListObject
{
int MyInt {get;set;}
string MyString {get;set;}
}And we have a list of "MyListObject" called "MyList". We also have an eventhandler that can trigger randomly. In that eventhandler a random item in "MyList" is removed from "MyList". Now, what happens if I do something like:
MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});
And at the same time we perform this operation on the list the event is triggered and the eventhandler removes some item from the list. Will that throw some kind of exception? I don't know how to test it so that's why I'm asking you guys. If this can cause problems, how can I get around this? Thanks for help!
Hi, at MSDN it is written that the enumeration is not thread-safe: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^] As far as I know removing an item from an collection that is enumerated at the moment throws an Exception (something like an InvalidOperationException). So to solve this problem I suggest using the lock statement: http://msdn.microsoft.com/en-us/library/c5kehkcz%28VS.80%29.aspx[^] Lock the access when removing something from the list, and lock when you iterate through the list. (Be sure to use the same lock-object!) Hope this helps. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.