@@ -47,6 +47,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
4747
4848 private CancellationTokenSource _updateSource ; // Used to cancel old query flows
4949 private CancellationToken _updateToken ; // Used to avoid ObjectDisposedException of _updateSource.Token
50+ private readonly object _updateSourceLock = new ( ) ;
5051
5152 private ChannelWriter < ResultsForUpdate > _resultsUpdateChannelWriter ;
5253 private Task _resultsViewUpdateTask ;
@@ -69,8 +70,11 @@ public MainViewModel()
6970 _queryText = "" ;
7071 _lastQuery = new Query ( ) ;
7172 _ignoredQueryText = null ; // null as invalid value
72- _updateSource = new CancellationTokenSource ( ) ;
73- _updateToken = _updateSource . Token ;
73+ lock ( _updateSourceLock )
74+ {
75+ _updateSource = new CancellationTokenSource ( ) ;
76+ _updateToken = _updateSource . Token ;
77+ }
7478
7579 Settings = Ioc . Default . GetRequiredService < Settings > ( ) ;
7680 Settings . PropertyChanged += ( _ , args ) =>
@@ -1240,7 +1244,10 @@ private static List<Result> GetHistoryItems(IEnumerable<HistoryItem> historyItem
12401244
12411245 private async Task QueryResultsAsync ( bool searchDelay , bool isReQuery = false , bool reSelect = true )
12421246 {
1243- _updateSource ? . Cancel ( ) ;
1247+ lock ( _updateSourceLock )
1248+ {
1249+ _updateSource . Cancel ( ) ;
1250+ }
12441251
12451252 App . API . LogDebug ( ClassName , $ "Start query with text: <{ QueryText } >") ;
12461253
@@ -1268,9 +1275,12 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12681275
12691276 var isHomeQuery = query . RawQuery == string . Empty ;
12701277
1271- _updateSource ? . Dispose ( ) ; // Dispose old update source to fix possible cancellation issue
1272- _updateSource = new CancellationTokenSource ( ) ;
1273- _updateToken = _updateSource . Token ;
1278+ lock ( _updateSourceLock )
1279+ {
1280+ _updateSource . Dispose ( ) ; // Dispose old update source to fix possible cancellation issue
1281+ _updateSource = new CancellationTokenSource ( ) ;
1282+ _updateToken = _updateSource . Token ;
1283+ }
12741284
12751285 ProgressBarVisibility = Visibility . Hidden ;
12761286 _isQueryRunning = true ;
@@ -1888,7 +1898,10 @@ protected virtual void Dispose(bool disposing)
18881898 {
18891899 if ( disposing )
18901900 {
1891- _updateSource ? . Dispose ( ) ;
1901+ lock ( _updateSourceLock )
1902+ {
1903+ _updateSource ? . Dispose ( ) ;
1904+ }
18921905 _resultsUpdateChannelWriter ? . Complete ( ) ;
18931906 if ( _resultsViewUpdateTask ? . IsCompleted == true )
18941907 {
0 commit comments