@@ -35,7 +35,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
35
35
private Query _lastQuery ;
36
36
private bool _lastIsHomeQuery ;
37
37
private string _queryTextBeforeLeaveResults ;
38
- private string _ignoredQueryText = null ;
38
+ private string _ignoredQueryText ; // Used to ignore query text change when switching between context menu and query results
39
39
40
40
private readonly FlowLauncherJsonStorage < History > _historyItemsStorage ;
41
41
private readonly FlowLauncherJsonStorage < UserSelectedRecord > _userSelectedRecordStorage ;
@@ -46,6 +46,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
46
46
private readonly TopMostRecord _topMostRecord ;
47
47
48
48
private CancellationTokenSource _updateSource ; // Used to cancel old query flows
49
+ private CancellationToken _updateToken ; // Used to avoid ObjectDisposedException of _updateSource.Token
49
50
50
51
private ChannelWriter < ResultsForUpdate > _resultsUpdateChannelWriter ;
51
52
private Task _resultsViewUpdateTask ;
@@ -67,6 +68,7 @@ public MainViewModel()
67
68
_queryTextBeforeLeaveResults = "" ;
68
69
_queryText = "" ;
69
70
_lastQuery = new Query ( ) ;
71
+ _ignoredQueryText = null ; // null as invalid value
70
72
71
73
Settings = Ioc . Default . GetRequiredService < Settings > ( ) ;
72
74
Settings . PropertyChanged += ( _ , args ) =>
@@ -248,7 +250,7 @@ public void RegisterResultsUpdatedEvent()
248
250
return ;
249
251
}
250
252
251
- var token = e . Token == default ? _updateSource . Token : e . Token ;
253
+ var token = e . Token == default ? _updateToken : e . Token ;
252
254
253
255
// make a clone to avoid possible issue that plugin will also change the list and items when updating view model
254
256
var resultsCopy = DeepCloneResults ( e . Results , token ) ;
@@ -1264,15 +1266,20 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1264
1266
1265
1267
var isHomeQuery = query . RawQuery == string . Empty ;
1266
1268
1267
- _updateSource = new CancellationTokenSource ( ) ;
1269
+ _updateSource ? . Dispose ( ) ;
1270
+
1271
+ var currentUpdateSource = new CancellationTokenSource ( ) ;
1272
+ _updateSource = currentUpdateSource ;
1273
+ var currentCancellationToken = _updateSource . Token ;
1274
+ _updateToken = currentCancellationToken ;
1268
1275
1269
1276
ProgressBarVisibility = Visibility . Hidden ;
1270
1277
_isQueryRunning = true ;
1271
1278
1272
1279
// Switch to ThreadPool thread
1273
1280
await TaskScheduler . Default ;
1274
1281
1275
- if ( _updateSource . Token . IsCancellationRequested ) return ;
1282
+ if ( currentCancellationToken . IsCancellationRequested ) return ;
1276
1283
1277
1284
// Update the query's IsReQuery property to true if this is a re-query
1278
1285
query . IsReQuery = isReQuery ;
@@ -1321,20 +1328,19 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1321
1328
{
1322
1329
// Wait 15 millisecond for query change in global query
1323
1330
// if query changes, return so that it won't be calculated
1324
- await Task.Delay(15, _updateSource.Token);
1325
- if (_updateSource.Token.IsCancellationRequested)
1326
- return;
1331
+ await Task.Delay(15, currentCancellationToken);
1332
+ if (currentCancellationToken.IsCancellationRequested) return;
1327
1333
}*/
1328
1334
1329
- _ = Task . Delay ( 200 , _updateSource . Token ) . ContinueWith ( _ =>
1335
+ _ = Task . Delay ( 200 , currentCancellationToken ) . ContinueWith ( _ =>
1330
1336
{
1331
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
1332
1338
if ( _isQueryRunning )
1333
1339
{
1334
1340
ProgressBarVisibility = Visibility . Visible ;
1335
1341
}
1336
1342
} ,
1337
- _updateSource . Token ,
1343
+ currentCancellationToken ,
1338
1344
TaskContinuationOptions . NotOnCanceled ,
1339
1345
TaskScheduler . Default ) ;
1340
1346
@@ -1345,21 +1351,21 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1345
1351
{
1346
1352
tasks = plugins . Select ( plugin => plugin . Metadata . HomeDisabled switch
1347
1353
{
1348
- false => QueryTaskAsync ( plugin , _updateSource . Token ) ,
1354
+ false => QueryTaskAsync ( plugin , currentCancellationToken ) ,
1349
1355
true => Task . CompletedTask
1350
1356
} ) . ToArray ( ) ;
1351
1357
1352
1358
// Query history results for home page firstly so it will be put on top of the results
1353
1359
if ( Settings . ShowHistoryResultsForHomePage )
1354
1360
{
1355
- QueryHistoryTask ( ) ;
1361
+ QueryHistoryTask ( currentCancellationToken ) ;
1356
1362
}
1357
1363
}
1358
1364
else
1359
1365
{
1360
1366
tasks = plugins . Select ( plugin => plugin . Metadata . Disabled switch
1361
1367
{
1362
- false => QueryTaskAsync ( plugin , _updateSource . Token ) ,
1368
+ false => QueryTaskAsync ( plugin , currentCancellationToken ) ,
1363
1369
true => Task . CompletedTask
1364
1370
} ) . ToArray ( ) ;
1365
1371
}
@@ -1374,13 +1380,13 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1374
1380
// nothing to do here
1375
1381
}
1376
1382
1377
- if ( _updateSource . Token . IsCancellationRequested ) return ;
1383
+ if ( currentCancellationToken . IsCancellationRequested ) return ;
1378
1384
1379
1385
// this should happen once after all queries are done so progress bar should continue
1380
1386
// until the end of all querying
1381
1387
_isQueryRunning = false ;
1382
1388
1383
- if ( ! _updateSource . Token . IsCancellationRequested )
1389
+ if ( ! currentCancellationToken . IsCancellationRequested )
1384
1390
{
1385
1391
// update to hidden if this is still the current query
1386
1392
ProgressBarVisibility = Visibility . Hidden ;
@@ -1440,19 +1446,19 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
1440
1446
}
1441
1447
}
1442
1448
1443
- void QueryHistoryTask ( )
1449
+ void QueryHistoryTask ( CancellationToken token )
1444
1450
{
1445
1451
// Select last history results and revert its order to make sure last history results are on top
1446
1452
var historyItems = _history . Items . TakeLast ( Settings . MaxHistoryResultsToShowForHomePage ) . Reverse ( ) ;
1447
1453
1448
1454
var results = GetHistoryItems ( historyItems ) ;
1449
1455
1450
- if ( _updateSource . Token . IsCancellationRequested ) return ;
1456
+ if ( token . IsCancellationRequested ) return ;
1451
1457
1452
1458
App . API . LogDebug ( ClassName , $ "Update results for history") ;
1453
1459
1454
1460
if ( ! _resultsUpdateChannelWriter . TryWrite ( new ResultsForUpdate ( results , _historyMetadata , query ,
1455
- _updateSource . Token ) ) )
1461
+ token ) ) )
1456
1462
{
1457
1463
App . API . LogError ( ClassName , "Unable to add item to Result Update Queue" ) ;
1458
1464
}
0 commit comments