From 01158fdc66bff9b8a64ff132e50807d4e317cc95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Fri, 4 Dec 2020 21:53:34 +0800 Subject: [PATCH] Add error handling for the resultUpdateTask to make sure result update can continue when error throw. --- Flow.Launcher/ViewModel/MainViewModel.cs | 41 ++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 9316cd62e..dd790a98b 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -24,6 +24,7 @@ using Flow.Launcher.Infrastructure.Image; using System.Collections.Concurrent; using Flow.Launcher.Infrastructure.Logger; using System.Threading.Tasks.Dataflow; +using NLog; namespace Flow.Launcher.ViewModel { @@ -51,6 +52,7 @@ namespace Flow.Launcher.ViewModel private readonly Internationalization _translator = InternationalizationManager.Instance; private BufferBlock _resultsUpdateQueue; + private Task _resultsViewUpdateTask; #endregion @@ -80,7 +82,7 @@ namespace Flow.Launcher.ViewModel InitializeKeyCommands(); - RegisterResultUpdate(); + RegisterViewUpdate(); RegisterResultsUpdatedEvent(); @@ -103,20 +105,31 @@ namespace Flow.Launcher.ViewModel } } - private void RegisterResultUpdate() + private void RegisterViewUpdate() { _resultsUpdateQueue = new BufferBlock(); + _resultsViewUpdateTask = Task.Run(updateAction).ContinueWith(continueAction, TaskContinuationOptions.OnlyOnFaulted); - Task.Run(async () => + + async Task updateAction() { while (await _resultsUpdateQueue.OutputAvailableAsync()) { await Task.Delay(20); _resultsUpdateQueue.TryReceiveAll(out var queue); UpdateResultView(queue.Where(r => !r.Token.IsCancellationRequested)); - } - }); + }; + + void continueAction(Task t) + { +#if DEBUG + throw t.Exception; +#else + Log.Error($"Error happen in task dealing with viewupdate for results. {t.Exception}"); + _resultsViewUpdateTask = Task.Run(updateAction).ContinueWith(continuationAction, TaskContinuationOptions.OnlyOnFaulted); +#endif + } } @@ -707,7 +720,23 @@ namespace Flow.Launcher.ViewModel { if (!resultsForUpdates.Any()) return; - CancellationToken token = resultsForUpdates.Select(r => r.Token).Distinct().Single(); + CancellationToken token; + + try + { + token = resultsForUpdates.Select(r => r.Token).Distinct().Single(); + } +#if DEBUG + catch + { + throw new ArgumentException("Unacceptable token"); + } +#else + catch + { + + } +#endif foreach (var result in resultsForUpdates.SelectMany(u => u.Results))