Skip to content

Commit 0717933

Browse files
authored
Fix progress query management and thread safety (#4135)
1 parent 516059e commit 0717933

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.ComponentModel;
45
using System.Globalization;
@@ -37,7 +38,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable, IResultUp
3738

3839
private Query _lastQuery;
3940
private bool _previousIsHomeQuery;
40-
private Query _progressQuery; // Used for QueryResultAsync
41+
private readonly ConcurrentDictionary<Guid, Query> _progressQueryDict = new(); // Used for QueryResultAsync
4142
private Query _updateQuery; // Used for ResultsUpdated
4243
private string _queryTextBeforeLeaveResults;
4344
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
@@ -1415,6 +1416,9 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
14151416
return;
14161417
}
14171418

1419+
// Create a Guid for this update session so that we can filter out in progress checking
1420+
var updateGuid = Guid.NewGuid();
1421+
14181422
try
14191423
{
14201424
_updateSource?.Dispose();
@@ -1426,7 +1430,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
14261430

14271431
ProgressBarVisibility = Visibility.Hidden;
14281432

1429-
_progressQuery = query;
1433+
_progressQueryDict.TryAdd(updateGuid, query);
14301434
_updateQuery = query;
14311435

14321436
// Switch to ThreadPool thread
@@ -1481,7 +1485,8 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
14811485
_ = Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
14821486
{
14831487
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
1484-
if (_progressQuery != null && _progressQuery.OriginalQuery == query.OriginalQuery)
1488+
if (_progressQueryDict.TryGetValue(updateGuid, out var progressQuery) &&
1489+
progressQuery.OriginalQuery == query.OriginalQuery)
14851490
{
14861491
ProgressBarVisibility = Visibility.Visible;
14871492
}
@@ -1537,7 +1542,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
15371542

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

15421547
if (!currentCancellationToken.IsCancellationRequested)
15431548
{
@@ -1547,8 +1552,8 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
15471552
}
15481553
finally
15491554
{
1550-
// this make sures progress query is null when this query is canceled
1551-
_progressQuery = null;
1555+
// this ensures the query is removed from the progress tracking dictionary when this query is canceled or completes
1556+
_progressQueryDict.Remove(updateGuid, out _);
15521557
}
15531558

15541559
// Local function

0 commit comments

Comments
 (0)