Skip to content

Commit

Permalink
fix: avoid collection modified while iterating
Browse files Browse the repository at this point in the history
clone the collection when there are async invocations in the loop body
  • Loading branch information
huynhsontung committed Nov 21, 2024
1 parent af25154 commit a6eff9d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Screenbox.Core/ViewModels/MediaListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -454,7 +454,7 @@ private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs
private async Task<PlaylistCreateResult?> CreatePlaylistAsync(IReadOnlyList<IStorageItem> files)
{
List<MediaViewModel> 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);
Expand Down

0 comments on commit a6eff9d

Please sign in to comment.