From eb13a18d52d1e0659e8896d3bfc4dc3961fad445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Thu, 3 Dec 2020 15:38:00 +0800 Subject: [PATCH] Use task.run in addresult instead of CollectionChange event. --- Flow.Launcher/ViewModel/ResultsViewModel.cs | 45 +++++++++++++-------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index 40021690b..924fd7ffc 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -150,11 +150,20 @@ namespace Flow.Launcher.ViewModel // https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ff71969-f183-4744-909d-50f7cd414954/binding-a-tabcontrols-selectedindex-not-working?forum=wpf // fix selected index flow + var updateTask = Task.Run(() => + { + // update UI in one run, so it can avoid UI flickering + + Results.Update(newResults); + if (Results.Any()) + SelectedItem = Results[0]; + }); + if (!updateTask.Wait(300)) + { + updateTask.Dispose(); + throw new TimeoutException("Update result use too much time."); + } - // update UI in one run, so it can avoid UI flickering - Results.Update(newResults); - if (Results.Any()) - SelectedItem = Results[0]; } if (Visbility != Visibility.Visible && Results.Count > 0) @@ -179,14 +188,23 @@ namespace Flow.Launcher.ViewModel return; lock (_collectionLock) { + // https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ff71969-f183-4744-909d-50f7cd414954/binding-a-tabcontrols-selectedindex-not-working?forum=wpf // fix selected index flow + var updateTask = Task.Run(() => + { + // update UI in one run, so it can avoid UI flickering + + Results.Update(newResults, token); + if (Results.Any()) + SelectedItem = Results[0]; + }); + if (!updateTask.Wait(300)) + { + updateTask.Dispose(); + throw new TimeoutException("Update result use too much time."); + } - Results.Update(newResults, token); - if (token.IsCancellationRequested) - return; - if (Results.Any()) - SelectedItem = Results[0]; } switch (Visbility) @@ -282,14 +300,7 @@ namespace Flow.Launcher.ViewModel { if (!_suppressNotifying) { - var notifyChangeTask = Task.Run(() => base.OnCollectionChanged(e)); - if (notifyChangeTask.Wait(300)) - return; - else - { - notifyChangeTask.Dispose(); - throw new TimeoutException(); - } + base.OnCollectionChanged(e); } }