I see a random exception "The collection has been modified, the enumeration cannot be executed" - InvalidOperationException.
The exception points to the foreach line in the code snippet below, I know that this happens when the collection changes when enumerated.
However, in my scenario, I see no real chance of this - a UNLESS private member is not thread safe .. Maybe I'm wrong, but that's when I need help to understand and understand .
This is what my code looks like
I have code behind a class that has a private collection, like
private Dictionary<string, string> _someDictionary = SomeConstantClass.ConstantValue;
In the page prerender completion event, I list the dictionary
protected override void OnPagePreRenderComplete(object sender, EventArgs e){ _someDictionary["AnotherKey"] = "Another value"; foreach(var dataValuePair in _SomeDictionary){
I also have a public property that can modify this collection, but it is set in the ascx file, for example
<tc: UserControlA runat="server" id="abc" CustomProperty="true" />
and here is its implementation,
public bool CustomProperty{ set{ if (value) _someDictionary["CustomProperty"] = "Custom Value"; } }
This has certainly changed my collection of member variables. But, as I understand it, this property must be started and executed in the Management Initiative itself.
So, I still do not see a scenario in which the collection changes during the pre render complete event.
Any idea what might throw an exception?
other notes: there are many update panels on the page, although this specific user control does nothing interesting and does not even have a postback script. From the log, I see that the problem occurs in the HTTP GET request to the page.
Moreover: suggest me a way (if any) to reproduce this.
For my friends who were interested to know SomeConstantClass.ConstantValue, here it is
class SomeConstantClass{ public static Dictionary<string, string> ConstantValue = new Dictionary<string, string> { {"ABCD", "EFGH"}, {"HIJK", "LMNO"} }; }