Skip to content
17 changes: 11 additions & 6 deletions Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
Expand Down Expand Up @@ -37,7 +38,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable, IResultUp

private Query _lastQuery;
private bool _previousIsHomeQuery;
private Query _progressQuery; // Used for QueryResultAsync
private readonly ConcurrentDictionary<Guid, Query> _progressQueryDict = new(); // Used for QueryResultAsync
private Query _updateQuery; // Used for ResultsUpdated
private string _queryTextBeforeLeaveResults;
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
Expand Down Expand Up @@ -1406,6 +1407,9 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
return;
}

// Create a Guid for this update session so that we can filter out in progress checking
var updateGuid = Guid.NewGuid();

try
{
_updateSource?.Dispose();
Expand All @@ -1417,7 +1421,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b

ProgressBarVisibility = Visibility.Hidden;

_progressQuery = query;
_progressQueryDict.TryAdd(updateGuid, query);
_updateQuery = query;

// Switch to ThreadPool thread
Expand Down Expand Up @@ -1472,7 +1476,8 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
_ = Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
{
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
if (_progressQuery != null && _progressQuery.OriginalQuery == query.OriginalQuery)
if (_progressQueryDict.TryGetValue(updateGuid, out var progressQuery) &&
progressQuery.OriginalQuery == query.OriginalQuery)
{
ProgressBarVisibility = Visibility.Visible;
}
Expand Down Expand Up @@ -1528,7 +1533,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b

// this should happen once after all queries are done so progress bar should continue
// until the end of all querying
_progressQuery = null;
_progressQueryDict.Remove(updateGuid, out _);

if (!currentCancellationToken.IsCancellationRequested)
{
Expand All @@ -1538,8 +1543,8 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
}
finally
{
// this make sures progress query is null when this query is canceled
_progressQuery = null;
// this ensures the query is removed from the progress tracking dictionary when this query is canceled or completes
_progressQueryDict.Remove(updateGuid, out _);
}

// Local function
Expand Down
Loading