diff --git a/LightlessSync/UI/LightFinderUI.cs b/LightlessSync/UI/LightFinderUI.cs index e41d17d..09ba646 100644 --- a/LightlessSync/UI/LightFinderUI.cs +++ b/LightlessSync/UI/LightFinderUI.cs @@ -497,7 +497,7 @@ public class LightFinderUI : WindowMediatorSubscriberBase var cardData = new List<(GroupJoinDto Shell, string BroadcasterName, bool IsOwnBroadcast)>(); - foreach (var shell in _nearbySyncshells) + foreach (var shell in _nearbySyncshells.ToArray()) { if (shell?.Group == null || string.IsNullOrEmpty(shell.Group.GID)) continue; @@ -1566,11 +1566,20 @@ public class LightFinderUI : WindowMediatorSubscriberBase if (previousGid != null) { - var newIndex = _nearbySyncshells.FindIndex(s => string.Equals(s.Group.GID, previousGid, StringComparison.Ordinal)); - if (newIndex >= 0) + try { - _selectedNearbyIndex = newIndex; - return; + var nearbySyncshellsSnapshot = _nearbySyncshells.ToArray(); + var newIndex = Array.FindIndex(nearbySyncshellsSnapshot, + s => string.Equals(s.Group.GID, previousGid, StringComparison.Ordinal)); + if (newIndex >= 0) + { + _selectedNearbyIndex = newIndex; + return; + } + } + catch + { + ClearSelection(); } } @@ -1612,9 +1621,18 @@ public class LightFinderUI : WindowMediatorSubscriberBase private string? GetSelectedGid() { - if (_selectedNearbyIndex < 0 || _selectedNearbyIndex >= _nearbySyncshells.Count) + try + { + var index = _selectedNearbyIndex; + var list = _nearbySyncshells.ToArray(); + if (index < 0 || index >= list.Length) + return null; + return list[index].Group.GID; + } + catch + { return null; - return _nearbySyncshells[_selectedNearbyIndex].Group.GID; + } } #endregion