From a6eff9d6cfab7db6c7b3bbdce875ffd4b3888ecd Mon Sep 17 00:00:00 2001 From: Tung Huynh Date: Wed, 20 Nov 2024 23:59:53 -0800 Subject: [PATCH] fix: avoid collection modified while iterating clone the collection when there are async invocations in the loop body --- Screenbox.Core/ViewModels/MediaListViewModel.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Screenbox.Core/ViewModels/MediaListViewModel.cs b/Screenbox.Core/ViewModels/MediaListViewModel.cs index 5b7b559b2..34c06b194 100644 --- a/Screenbox.Core/ViewModels/MediaListViewModel.cs +++ b/Screenbox.Core/ViewModels/MediaListViewModel.cs @@ -182,7 +182,7 @@ public async void Receive(QueuePlaylistMessage message) _lastUpdated = message.Value; bool canInsert = CurrentIndex + 1 < Items.Count; int counter = 0; - foreach (MediaViewModel media in message.Value) + foreach (MediaViewModel media in message.Value.ToList()) // ToList() to avoid modifying the collection while iterating { var result = await CreatePlaylistAsync(media); foreach (MediaViewModel subMedia in result.Playlist) @@ -454,7 +454,7 @@ private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs private async Task CreatePlaylistAsync(IReadOnlyList files) { List queue = new(); - foreach (IStorageItem item in files) + foreach (IStorageItem item in files.ToList()) // ToList() to avoid modifying the collection while iterating { if (item is not StorageFile storageFile) continue; MediaViewModel vm = _mediaFactory.GetSingleton(storageFile);