From 4ca8cee91fb46977b75e8329c18d9b6a4654b12e Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sat, 7 Jul 2018 15:02:20 +0300 Subject: Working on PPC jobs loading performance... --- .../ObservableCollectionExtensions.cs | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/ExtensionMethods/ObservableCollectionExtensions.cs (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/ExtensionMethods/ObservableCollectionExtensions.cs') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/ExtensionMethods/ObservableCollectionExtensions.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/ExtensionMethods/ObservableCollectionExtensions.cs new file mode 100644 index 000000000..c539273d9 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/ExtensionMethods/ObservableCollectionExtensions.cs @@ -0,0 +1,53 @@ +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); + } + } +} + -- cgit v1.3.1