From ad20640f82df417ca21b63b25fc37a408e88518f Mon Sep 17 00:00:00 2001 From: CHEN HONG GUAN <666ddxx@gmail.com> Date: Tue, 25 Aug 2026 21:54:07 +0800 Subject: [PATCH] fix: prevent stale query preview updates Fixes #23 --- MainWindow.xaml.cs | 23 ++++++++++++++--------- tests/EventFast.Tests/Program.cs | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index d54b8d0..f336b41 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -107,7 +107,7 @@ private async Task RunQueryAsync(QuickQuery? quick, bool refresh = false) if (channels.Length == 0) { - StatusText.Text = Localization.Text("SelectChannel"); + SetOperationStatus(() => Localization.Text("SelectChannel")); return; } @@ -121,6 +121,7 @@ private async Task RunQueryAsync(QuickQuery? quick, bool refresh = false) SetOperationStatus(() => quick is null ? Localization.Text("Querying") : Localization.Format("QueryingNamed", Localization.Text(quick.Name))); + var queryCompleted = false; try { @@ -142,7 +143,7 @@ void ShowFirstBatch(IReadOnlyList batch) var filtered = batch.Where(row => EventQuery.Matches(row, criteria)).ToArray(); Dispatcher.BeginInvoke(() => { - if (token.IsCancellationRequested) + if (!CanShowFirstBatch(_operationCancellation, operation, queryCompleted, token)) return; previewRows.AddRange(filtered); EventsGrid.ItemsSource = ProblemGrouping.Group(previewRows); @@ -171,6 +172,7 @@ void ShowFirstBatch(IReadOnlyList batch) token.ThrowIfCancellationRequested(); var groups = ProblemGrouping.Group(rows); token.ThrowIfCancellationRequested(); + queryCompleted = true; _rows = rows; _allRows = allRows; _groups = groups; @@ -180,27 +182,26 @@ void ShowFirstBatch(IReadOnlyList batch) var firstError = results.Select(result => result.Error).FirstOrDefault(error => error is not null); AdminButton.Visibility = results.Any(result => result.RequiresAdmin) ? Visibility.Visible : Visibility.Collapsed; StatusText.ToolTip = string.Join(Environment.NewLine, results.Select(result => result.Error).OfType()); - StatusText.Text = Localization.Format("QuerySummary", allRows.Length, rows.Length, - rows.Count(row => row.Level == "Critical"), rows.Count(row => row.Level == "Error"), - rows.Count(row => row.Level == "Warning"), groups.Count) + - (errors > 0 ? Localization.Format("SkippedChannels", errors, firstError) : ""); + SetOperationStatus(() => Localization.Format("QuerySummary", allRows.Length, rows.Length, + rows.Count(row => row.Level == "Critical"), rows.Count(row => row.Level == "Error"), + rows.Count(row => row.Level == "Warning"), groups.Count) + + (errors > 0 ? Localization.Format("SkippedChannels", errors, firstError) : "")); } catch (OperationCanceledException) { if (ReferenceEquals(_operationCancellation, operation)) - StatusText.Text = Localization.Text("QueryCancelled"); + SetOperationStatus(() => Localization.Text("QueryCancelled")); } catch (Exception exception) { if (ReferenceEquals(_operationCancellation, operation)) - StatusText.Text = exception.Message; + SetOperationStatus(() => exception.Message); } finally { if (ReferenceEquals(_operationCancellation, operation)) { _operationCancellation = null; - _operationStatus = null; operation.Dispose(); SearchButton.IsEnabled = true; CancelButton.IsEnabled = false; @@ -209,6 +210,10 @@ void ShowFirstBatch(IReadOnlyList batch) } } + internal static bool CanShowFirstBatch(CancellationTokenSource? current, CancellationTokenSource operation, + bool queryCompleted, CancellationToken token) => + !token.IsCancellationRequested && ReferenceEquals(current, operation) && !queryCompleted; + private async void Window_Drop(object sender, DragEventArgs e) { if (!e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetData(DataFormats.FileDrop) is not string[] { Length: > 0 } files || diff --git a/tests/EventFast.Tests/Program.cs b/tests/EventFast.Tests/Program.cs index 08e721f..d2cc338 100644 --- a/tests/EventFast.Tests/Program.cs +++ b/tests/EventFast.Tests/Program.cs @@ -17,6 +17,7 @@ ("XPath/time/level filters", TestXPath), ("Keyword and Event ID filters", TestFilters), ("Provider-aware quick filters", TestQuickFilters), + ("Query first-batch lifecycle", TestQueryFirstBatchLifecycle), ("Grouping/classifier/sorting", TestGrouping), ("Problem summary", TestProblemSummary), ("Event details layout", TestEventDetailsLayout), @@ -296,6 +297,17 @@ static void TestQuickFilters() ProblemClassifier.Classify("Fake-USB-Provider", 7) == "Fake-USB-Provider + Event ID 7"); } +static void TestQueryFirstBatchLifecycle() +{ + using var operation = new CancellationTokenSource(); + using var newerOperation = new CancellationTokenSource(); + Assert(MainWindow.CanShowFirstBatch(operation, operation, false, operation.Token)); + Assert(!MainWindow.CanShowFirstBatch(operation, operation, true, operation.Token)); + Assert(!MainWindow.CanShowFirstBatch(newerOperation, operation, false, operation.Token)); + operation.Cancel(); + Assert(!MainWindow.CanShowFirstBatch(operation, operation, false, operation.Token)); +} + static void TestGrouping() { var now = DateTime.Now; @@ -652,7 +664,11 @@ eventsGrid.SelectedItem is ProblemGroup && if (searchEnabled && !status.Contains("Querying", StringComparison.Ordinal)) { var eventIds = eventsGrid.Items.Cast().Select(group => group.EventId).ToArray(); - Assert(eventIds.SequenceEqual(eventIds.Order())); + Assert(eventIds.SequenceEqual(eventIds.Order()) && status.StartsWith("Scanned", StringComparison.Ordinal)); + window.ApplyLanguage("zh-TW", persist: false); + var localizedStatus = ((TextBlock)window.FindName("StatusText")).Text; + Assert(localizedStatus.StartsWith("掃描", StringComparison.Ordinal) && + !localizedStatus.Contains("背景查詢中", StringComparison.Ordinal)); timer.Stop(); window.Close(); app.Shutdown();