why does this lock test not work? he throws an exception below Console.Write that the collection has been modified ....
static List<string> staticVar = new List<string>(); static void Main(string[] args) { Action<IEnumerable<int>> assyncMethod = enumerator => { lock (staticVar) foreach (int item in enumerator) staticVar.Add(item.ToString()); }; assyncMethod.BeginInvoke(Enumerable.Range(0, 500000), null, null); Thread.Sleep(100); Console.Write(staticVar.Count()); foreach (string item in staticVar) { } }
source share