From 69e1d3ea3ec1fd578601f75027da4d66ecc56e71 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Wed, 27 Dec 2023 12:32:11 -0600 Subject: [PATCH] Add a lock on the reload method --- Maui.DataGrid/DataGrid.xaml.cs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Maui.DataGrid/DataGrid.xaml.cs b/Maui.DataGrid/DataGrid.xaml.cs index 43a1b3e..3c561d7 100644 --- a/Maui.DataGrid/DataGrid.xaml.cs +++ b/Maui.DataGrid/DataGrid.xaml.cs @@ -33,6 +33,7 @@ public partial class DataGrid private readonly Style _defaultSortIconStyle; private bool _isReloading; + private readonly object _reloadLock = new(); private IList? _internalItems; private DataGridColumn? _sortedColumn; @@ -1034,26 +1035,29 @@ private void OnSelectionChanged(object? sender, SelectionChangedEventArgs e) internal void Reload() { - if (_isReloading) + lock (_reloadLock) { - return; - } + if (_isReloading) + { + return; + } - _isReloading = true; + _isReloading = true; - try - { - InitHeaderView(); + try + { + InitHeaderView(); - if (_internalItems is not null) + if (_internalItems is not null) + { + InternalItems = new List(_internalItems); + } + } + finally { - InternalItems = new List(_internalItems); + _isReloading = false; } } - finally - { - _isReloading = false; - } } #endregion UI Methods