diff --git a/common/Environments/Helpers/ComputeSystemHelpers.cs b/common/Environments/Helpers/ComputeSystemHelpers.cs index 97f5e57ab1..e2635a4ee7 100644 --- a/common/Environments/Helpers/ComputeSystemHelpers.cs +++ b/common/Environments/Helpers/ComputeSystemHelpers.cs @@ -86,8 +86,8 @@ public static async Task> GetComputeSystemCardPropertiesAsync { try { - var curentProperties = await computeSystem.GetComputeSystemPropertiesAsync(string.Empty); - return GetComputeSystemCardProperties(curentProperties, packageFullName); + var currentProperties = await computeSystem.GetComputeSystemPropertiesAsync(string.Empty); + return GetComputeSystemCardProperties(currentProperties, packageFullName); } catch (Exception ex) { @@ -131,7 +131,7 @@ public static EnvironmentsCallToActionData UpdateCallToActionText(int providerCo } /// - /// Safely remove all items from an observable collection. + /// Safely removes all items from an observable collection and replaces them with new items. /// /// /// There can be random COM exceptions due to using the "Clear()" method in an observable collection. This method @@ -140,19 +140,32 @@ public static EnvironmentsCallToActionData UpdateCallToActionText(int providerCo /// this method is used to remove all items individually from the end of the collection to the beginning of the collection. /// /// Type of objects that the collection contains - /// An observable collection that contains zero to N elements - public static void RemoveAllItems(ObservableCollection collection) + /// An observable collection that contains zero to N elements that will have its contents replaced + /// A list that contains zero to N elements whose elements will be added to collectionToUpdate + /// + /// True only if we successfully replaced all items in the collection. False otherwise. + /// + public static bool RemoveAllItemsAndReplace(ObservableCollection collectionToUpdate, List listWithUpdates) { try { - for (var i = collection.Count - 1; i >= 0; i--) + for (var i = collectionToUpdate.Count - 1; i >= 0; i--) { - collection.RemoveAt(i); + collectionToUpdate.RemoveAt(i); } + + for (var i = 0; i < listWithUpdates.Count; i++) + { + collectionToUpdate.Add(listWithUpdates[i]); + } + + return true; } catch (Exception ex) { _log.Error(ex, "Unable to remove items from the collection"); } + + return false; } } diff --git a/tools/Environments/DevHome.Environments/ViewModels/ComputeSystemViewModel.cs b/tools/Environments/DevHome.Environments/ViewModels/ComputeSystemViewModel.cs index 9a13c27e94..04bb748b93 100644 --- a/tools/Environments/DevHome.Environments/ViewModels/ComputeSystemViewModel.cs +++ b/tools/Environments/DevHome.Environments/ViewModels/ComputeSystemViewModel.cs @@ -188,11 +188,9 @@ private async void SetPropertiesAsync() } var properties = await ComputeSystemHelpers.GetComputeSystemCardPropertiesAsync(ComputeSystem!, PackageFullName); - - ComputeSystemHelpers.RemoveAllItems(Properties); - foreach (var property in properties) + if (!ComputeSystemHelpers.RemoveAllItemsAndReplace(Properties, properties)) { - Properties.Add(property); + Properties = new(properties); } } diff --git a/tools/SetupFlow/DevHome.SetupFlow/ViewModels/Environments/ComputeSystemCardViewModel.cs b/tools/SetupFlow/DevHome.SetupFlow/ViewModels/Environments/ComputeSystemCardViewModel.cs index c6d49a13b1..b5b330412a 100644 --- a/tools/SetupFlow/DevHome.SetupFlow/ViewModels/Environments/ComputeSystemCardViewModel.cs +++ b/tools/SetupFlow/DevHome.SetupFlow/ViewModels/Environments/ComputeSystemCardViewModel.cs @@ -99,10 +99,9 @@ private async Task UpdatePropertiesAsync() var properties = await ComputeSystemHelpers.GetComputeSystemCardPropertiesAsync(ComputeSystem, _packageFullName); lock (_lock) { - ComputeSystemHelpers.RemoveAllItems(ComputeSystemProperties); - foreach (var property in properties) + if (!ComputeSystemHelpers.RemoveAllItemsAndReplace(ComputeSystemProperties, properties)) { - ComputeSystemProperties.Add(property); + ComputeSystemProperties = new(properties); } } }