using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; public static class ObservableCollectionExtensions { public static ObservableCollection ToObservableCollectionAsyncIdle(this ObservableCollection source) { var copy = source.ToList(); ObservableCollection result = new ObservableCollection(); foreach (var item in copy) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { result.Add(item); }), DispatcherPriority.ContextIdle); } return result; } public static void ReloadAsyncIdle(this ObservableCollection source, Action onProgress, Action onComplete = null) { var copy = source.ToList(); source.Clear(); int count = copy.Count; int completed = 0; foreach (var item in copy) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { source.Add(item); completed++; onProgress?.Invoke(completed, count); if (completed == count) { onComplete?.Invoke(); } }), DispatcherPriority.ContextIdle); } } }