please cleanup the logic make it better and fix the error InvalidOperationException:…
- User
please cleanup the logic make it better and fix the error InvalidOperationException: Collection was modified; enumeration operation may not execute.
private void ObjectProximityChecker() { while (isRunning) { // Update all orphans var orphannedKeys = this.orphannedPrims.Keys.ToList(); foreach (var key in orphannedKeys) { if (this.orphannedPrims.TryGetValue(key, out var prim)) { if (this.primPositions.TryGetValue(prim.Item1, out var position)) { var parentPosition = position; var primPosition = prim.Item2; var actualPosition = parentPosition + primPosition; if (this.primPositions.TryAdd(key, actualPosition)) { this.orphannedPrims.TryRemove(key, out _); } } } }
var toAddToQueue = new List<PrimEventArgs>(); var keys = this.allPrimObjects.Keys.ToList(); foreach (var key in keys) { if (this.allPrimObjects.TryGetValue(key, out List<PrimEventArgs> primEvent)) { if (primEvent.Any(IsInCameraView)) // ERROR HERE: InvalidOperationException: Collection was modified; enumeration operation may not execute. { toAddToQueue.AddRange(primEvent); } } } foreach (var primEvent in toAddToQueue) { this.objectsToProcessQueue.Enqueue(primEvent); } Thread.Sleep(_viewConfig.NewObjectPollMS); }}
- Assistant