@@ -1266,17 +1266,20 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1266
1266
1267
1267
var isHomeQuery = query . RawQuery == string . Empty ;
1268
1268
1269
- _updateSource ? . Dispose ( ) ; // Dispose old update source to fix possible cancellation issue
1270
- _updateSource = new CancellationTokenSource ( ) ;
1271
- _updateToken = _updateSource . Token ;
1269
+ _updateSource ? . Dispose ( ) ;
1270
+
1271
+ var currentUpdateSource = new CancellationTokenSource ( ) ;
1272
+ _updateSource = currentUpdateSource ;
1273
+ var currentCancellationToken = _updateSource . Token ;
1274
+ _updateToken = currentCancellationToken ;
1272
1275
1273
1276
ProgressBarVisibility = Visibility . Hidden ;
1274
1277
_isQueryRunning = true ;
1275
1278
1276
1279
// Switch to ThreadPool thread
1277
1280
await TaskScheduler . Default ;
1278
1281
1279
- if ( _updateToken . IsCancellationRequested ) return ;
1282
+ if ( currentCancellationToken . IsCancellationRequested ) return ;
1280
1283
1281
1284
// Update the query's IsReQuery property to true if this is a re-query
1282
1285
query . IsReQuery = isReQuery ;
@@ -1325,19 +1328,19 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1325
1328
{
1326
1329
// Wait 15 millisecond for query change in global query
1327
1330
// if query changes, return so that it won't be calculated
1328
- await Task.Delay(15, _updateToken );
1329
- if (_updateToken .IsCancellationRequested) return;
1331
+ await Task.Delay(15, currentCancellationToken );
1332
+ if (currentCancellationToken .IsCancellationRequested) return;
1330
1333
}*/
1331
1334
1332
- _ = Task . Delay ( 200 , _updateToken ) . ContinueWith ( _ =>
1335
+ _ = Task . Delay ( 200 , currentCancellationToken ) . ContinueWith ( _ =>
1333
1336
{
1334
1337
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
1335
1338
if ( _isQueryRunning )
1336
1339
{
1337
1340
ProgressBarVisibility = Visibility . Visible ;
1338
1341
}
1339
1342
} ,
1340
- _updateToken ,
1343
+ currentCancellationToken ,
1341
1344
TaskContinuationOptions . NotOnCanceled ,
1342
1345
TaskScheduler . Default ) ;
1343
1346
@@ -1348,21 +1351,21 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1348
1351
{
1349
1352
tasks = plugins . Select ( plugin => plugin . Metadata . HomeDisabled switch
1350
1353
{
1351
- false => QueryTaskAsync ( plugin , _updateToken ) ,
1354
+ false => QueryTaskAsync ( plugin , currentCancellationToken ) ,
1352
1355
true => Task . CompletedTask
1353
1356
} ) . ToArray ( ) ;
1354
1357
1355
1358
// Query history results for home page firstly so it will be put on top of the results
1356
1359
if ( Settings . ShowHistoryResultsForHomePage )
1357
1360
{
1358
- QueryHistoryTask ( ) ;
1361
+ QueryHistoryTask ( currentCancellationToken ) ;
1359
1362
}
1360
1363
}
1361
1364
else
1362
1365
{
1363
1366
tasks = plugins . Select ( plugin => plugin . Metadata . Disabled switch
1364
1367
{
1365
- false => QueryTaskAsync ( plugin , _updateToken ) ,
1368
+ false => QueryTaskAsync ( plugin , currentCancellationToken ) ,
1366
1369
true => Task . CompletedTask
1367
1370
} ) . ToArray ( ) ;
1368
1371
}
@@ -1377,13 +1380,13 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1377
1380
// nothing to do here
1378
1381
}
1379
1382
1380
- if ( _updateToken . IsCancellationRequested ) return ;
1383
+ if ( currentCancellationToken . IsCancellationRequested ) return ;
1381
1384
1382
1385
// this should happen once after all queries are done so progress bar should continue
1383
1386
// until the end of all querying
1384
1387
_isQueryRunning = false ;
1385
1388
1386
- if ( ! _updateToken . IsCancellationRequested )
1389
+ if ( ! currentCancellationToken . IsCancellationRequested )
1387
1390
{
1388
1391
// update to hidden if this is still the current query
1389
1392
ProgressBarVisibility = Visibility . Hidden ;
@@ -1443,19 +1446,19 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
1443
1446
}
1444
1447
}
1445
1448
1446
- void QueryHistoryTask ( )
1449
+ void QueryHistoryTask ( CancellationToken token )
1447
1450
{
1448
1451
// Select last history results and revert its order to make sure last history results are on top
1449
1452
var historyItems = _history . Items . TakeLast ( Settings . MaxHistoryResultsToShowForHomePage ) . Reverse ( ) ;
1450
1453
1451
1454
var results = GetHistoryItems ( historyItems ) ;
1452
1455
1453
- if ( _updateToken . IsCancellationRequested ) return ;
1456
+ if ( token . IsCancellationRequested ) return ;
1454
1457
1455
1458
App . API . LogDebug ( ClassName , $ "Update results for history") ;
1456
1459
1457
1460
if ( ! _resultsUpdateChannelWriter . TryWrite ( new ResultsForUpdate ( results , _historyMetadata , query ,
1458
- _updateToken ) ) )
1461
+ token ) ) )
1459
1462
{
1460
1463
App . API . LogError ( ClassName , "Unable to add item to Result Update Queue" ) ;
1461
1464
}
0 commit comments