Skip to content

Commit 7876fa1

Browse files
committed
Fix possible cancel token issue
1 parent 711865d commit 7876fa1

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,16 +1091,15 @@ private async Task QueryResultsAsync(bool isReQuery = false, bool reSelect = tru
10911091

10921092
var currentUpdateSource = new CancellationTokenSource();
10931093
_updateSource = currentUpdateSource;
1094-
var currentCancellationToken = _updateSource.Token;
1095-
_updateToken = currentCancellationToken;
1094+
_updateToken = _updateSource.Token;
10961095

10971096
ProgressBarVisibility = Visibility.Hidden;
10981097
_isQueryRunning = true;
10991098

11001099
// Switch to ThreadPool thread
11011100
await TaskScheduler.Default;
11021101

1103-
if (currentCancellationToken.IsCancellationRequested)
1102+
if (_updateSource.Token.IsCancellationRequested)
11041103
return;
11051104

11061105
// Update the query's IsReQuery property to true if this is a re-query
@@ -1126,24 +1125,23 @@ private async Task QueryResultsAsync(bool isReQuery = false, bool reSelect = tru
11261125
SearchIconVisibility = Visibility.Visible;
11271126
}
11281127

1129-
11301128
if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign)
11311129
{
11321130
// Wait 45 millisecond for query change in global query
11331131
// if query changes, return so that it won't be calculated
1134-
await Task.Delay(45, currentCancellationToken);
1135-
if (currentCancellationToken.IsCancellationRequested)
1132+
await Task.Delay(45, _updateSource.Token);
1133+
if (_updateSource.Token.IsCancellationRequested)
11361134
return;
11371135
}
11381136

1139-
_ = Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
1137+
_ = Task.Delay(200, _updateSource.Token).ContinueWith(_ =>
11401138
{
11411139
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
1142-
if (!currentCancellationToken.IsCancellationRequested && _isQueryRunning)
1140+
if (!_updateSource.Token.IsCancellationRequested && _isQueryRunning)
11431141
{
11441142
ProgressBarVisibility = Visibility.Visible;
11451143
}
1146-
}, currentCancellationToken, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default);
1144+
}, _updateSource.Token, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default);
11471145

11481146
// plugins is ICollection, meaning LINQ will get the Count and preallocate Array
11491147

@@ -1153,7 +1151,6 @@ private async Task QueryResultsAsync(bool isReQuery = false, bool reSelect = tru
11531151
true => Task.CompletedTask
11541152
}).ToArray();
11551153

1156-
11571154
try
11581155
{
11591156
// Check the code, WhenAll will translate all type of IEnumerable or Collection to Array, so make an array at first
@@ -1164,13 +1161,13 @@ private async Task QueryResultsAsync(bool isReQuery = false, bool reSelect = tru
11641161
// nothing to do here
11651162
}
11661163

1167-
if (currentCancellationToken.IsCancellationRequested)
1164+
if (_updateSource.Token.IsCancellationRequested)
11681165
return;
11691166

11701167
// this should happen once after all queries are done so progress bar should continue
11711168
// until the end of all querying
11721169
_isQueryRunning = false;
1173-
if (!currentCancellationToken.IsCancellationRequested)
1170+
if (!_updateSource.Token.IsCancellationRequested)
11741171
{
11751172
// update to hidden if this is still the current query
11761173
ProgressBarVisibility = Visibility.Hidden;
@@ -1184,9 +1181,9 @@ async Task QueryTaskAsync(PluginPair plugin, bool reSelect = true)
11841181
await Task.Yield();
11851182

11861183
IReadOnlyList<Result> results =
1187-
await PluginManager.QueryForPluginAsync(plugin, query, currentCancellationToken);
1184+
await PluginManager.QueryForPluginAsync(plugin, query, _updateSource.Token);
11881185

1189-
currentCancellationToken.ThrowIfCancellationRequested();
1186+
_updateSource.Token.ThrowIfCancellationRequested();
11901187

11911188
IReadOnlyList<Result> resultsCopy;
11921189
if (results == null)
@@ -1200,7 +1197,7 @@ async Task QueryTaskAsync(PluginPair plugin, bool reSelect = true)
12001197
}
12011198

12021199
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
1203-
currentCancellationToken, reSelect)))
1200+
_updateSource.Token, reSelect)))
12041201
{
12051202
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
12061203
}

0 commit comments

Comments
 (0)