@@ -35,7 +35,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
3535 private Query _lastQuery ;
3636 private bool _lastIsHomeQuery ;
3737 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
3939
4040 private readonly FlowLauncherJsonStorage < History > _historyItemsStorage ;
4141 private readonly FlowLauncherJsonStorage < UserSelectedRecord > _userSelectedRecordStorage ;
@@ -46,6 +46,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
4646 private readonly TopMostRecord _topMostRecord ;
4747
4848 private CancellationTokenSource _updateSource ; // Used to cancel old query flows
49+ private CancellationToken _updateToken ; // Used to avoid ObjectDisposedException of _updateSource.Token
4950
5051 private ChannelWriter < ResultsForUpdate > _resultsUpdateChannelWriter ;
5152 private Task _resultsViewUpdateTask ;
@@ -67,6 +68,7 @@ public MainViewModel()
6768 _queryTextBeforeLeaveResults = "" ;
6869 _queryText = "" ;
6970 _lastQuery = new Query ( ) ;
71+ _ignoredQueryText = null ; // null as invalid value
7072
7173 Settings = Ioc . Default . GetRequiredService < Settings > ( ) ;
7274 Settings . PropertyChanged += ( _ , args ) =>
@@ -248,7 +250,7 @@ public void RegisterResultsUpdatedEvent()
248250 return ;
249251 }
250252
251- var token = e . Token == default ? _updateSource . Token : e . Token ;
253+ var token = e . Token == default ? _updateToken : e . Token ;
252254
253255 // make a clone to avoid possible issue that plugin will also change the list and items when updating view model
254256 var resultsCopy = DeepCloneResults ( e . Results , token ) ;
@@ -1264,15 +1266,20 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12641266
12651267 var isHomeQuery = query . RawQuery == string . Empty ;
12661268
1267- _updateSource = new CancellationTokenSource ( ) ;
1269+ _updateSource ? . Dispose ( ) ;
1270+
1271+ var currentUpdateSource = new CancellationTokenSource ( ) ;
1272+ _updateSource = currentUpdateSource ;
1273+ var currentCancellationToken = _updateSource . Token ;
1274+ _updateToken = currentCancellationToken ;
12681275
12691276 ProgressBarVisibility = Visibility . Hidden ;
12701277 _isQueryRunning = true ;
12711278
12721279 // Switch to ThreadPool thread
12731280 await TaskScheduler . Default ;
12741281
1275- if ( _updateSource . Token . IsCancellationRequested ) return ;
1282+ if ( currentCancellationToken . IsCancellationRequested ) return ;
12761283
12771284 // Update the query's IsReQuery property to true if this is a re-query
12781285 query . IsReQuery = isReQuery ;
@@ -1321,20 +1328,19 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13211328 {
13221329 // Wait 15 millisecond for query change in global query
13231330 // 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;
13271333 }*/
13281334
1329- _ = Task . Delay ( 200 , _updateSource . Token ) . ContinueWith ( _ =>
1335+ _ = Task . Delay ( 200 , currentCancellationToken ) . ContinueWith ( _ =>
13301336 {
13311337 // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
13321338 if ( _isQueryRunning )
13331339 {
13341340 ProgressBarVisibility = Visibility . Visible ;
13351341 }
13361342 } ,
1337- _updateSource . Token ,
1343+ currentCancellationToken ,
13381344 TaskContinuationOptions . NotOnCanceled ,
13391345 TaskScheduler . Default ) ;
13401346
@@ -1345,21 +1351,21 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13451351 {
13461352 tasks = plugins . Select ( plugin => plugin . Metadata . HomeDisabled switch
13471353 {
1348- false => QueryTaskAsync ( plugin , _updateSource . Token ) ,
1354+ false => QueryTaskAsync ( plugin , currentCancellationToken ) ,
13491355 true => Task . CompletedTask
13501356 } ) . ToArray ( ) ;
13511357
13521358 // Query history results for home page firstly so it will be put on top of the results
13531359 if ( Settings . ShowHistoryResultsForHomePage )
13541360 {
1355- QueryHistoryTask ( ) ;
1361+ QueryHistoryTask ( currentCancellationToken ) ;
13561362 }
13571363 }
13581364 else
13591365 {
13601366 tasks = plugins . Select ( plugin => plugin . Metadata . Disabled switch
13611367 {
1362- false => QueryTaskAsync ( plugin , _updateSource . Token ) ,
1368+ false => QueryTaskAsync ( plugin , currentCancellationToken ) ,
13631369 true => Task . CompletedTask
13641370 } ) . ToArray ( ) ;
13651371 }
@@ -1374,13 +1380,13 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13741380 // nothing to do here
13751381 }
13761382
1377- if ( _updateSource . Token . IsCancellationRequested ) return ;
1383+ if ( currentCancellationToken . IsCancellationRequested ) return ;
13781384
13791385 // this should happen once after all queries are done so progress bar should continue
13801386 // until the end of all querying
13811387 _isQueryRunning = false ;
13821388
1383- if ( ! _updateSource . Token . IsCancellationRequested )
1389+ if ( ! currentCancellationToken . IsCancellationRequested )
13841390 {
13851391 // update to hidden if this is still the current query
13861392 ProgressBarVisibility = Visibility . Hidden ;
@@ -1440,19 +1446,19 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14401446 }
14411447 }
14421448
1443- void QueryHistoryTask ( )
1449+ void QueryHistoryTask ( CancellationToken token )
14441450 {
14451451 // Select last history results and revert its order to make sure last history results are on top
14461452 var historyItems = _history . Items . TakeLast ( Settings . MaxHistoryResultsToShowForHomePage ) . Reverse ( ) ;
14471453
14481454 var results = GetHistoryItems ( historyItems ) ;
14491455
1450- if ( _updateSource . Token . IsCancellationRequested ) return ;
1456+ if ( token . IsCancellationRequested ) return ;
14511457
14521458 App . API . LogDebug ( ClassName , $ "Update results for history") ;
14531459
14541460 if ( ! _resultsUpdateChannelWriter . TryWrite ( new ResultsForUpdate ( results , _historyMetadata , query ,
1455- _updateSource . Token ) ) )
1461+ token ) ) )
14561462 {
14571463 App . API . LogError ( ClassName , "Unable to add item to Result Update Queue" ) ;
14581464 }
0 commit comments