This approach will not work. You may not get an exception, you may just end up looping forever inside the dictionary. This is not a theoretical issue, it is something which does happen in the wild when you mutate a dictionary while iterating it.
Alan On 13 February 2013 16:08, edward.harvey.mono <edward.harvey.m...@clevertrove.com> wrote: > I have a dictionary, and I want to read it from one thread, while possibly > modifying it from another thread. In the event when one thread modifies it > while another thread is enumerating it, I actually *want* the change to > occur, and I want the enumerating thread to be aware a change has occurred. > > > > The best way I know is to let the exception occur, and simply handle the > exception. > > > > Problem is, the exception type is InvalidOperationException, which is pretty > generic. So I need to handle InvalidOperationException, and look inside it, > to see if "Collection was modified" is the cause of the exception. Right > now, the only way I know to do it is like this: > > > > catch (InvalidOperationException e) > > { > > if (e.Message == "Collection was modified; enumeration > operation may not execute.") > > > > This seems a little kludgy... First of all, can I be guaranteed the string > will remain exactly the same, across platforms, and different versions of > .Net and mono? Second of all, it's a string comparison. If it works, I > don't mind it. It will happen rarely enough that I'm willing to pay the > performance of string comparisons. The important goal is to accurately and > reliably catch the right exception, on different platforms. > > > > Thanks for suggestions... > > > _______________________________________________ > Mono-list maillist - Mono-list@lists.ximian.com > http://lists.ximian.com/mailman/listinfo/mono-list > _______________________________________________ Mono-list maillist - Mono-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-list