@@ -47,6 +47,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
47
47
48
48
private CancellationTokenSource _updateSource ; // Used to cancel old query flows
49
49
private CancellationToken _updateToken ; // Used to avoid ObjectDisposedException of _updateSource.Token
50
+ private readonly object _updateSourceLock = new ( ) ;
50
51
51
52
private ChannelWriter < ResultsForUpdate > _resultsUpdateChannelWriter ;
52
53
private Task _resultsViewUpdateTask ;
@@ -69,8 +70,11 @@ public MainViewModel()
69
70
_queryText = "" ;
70
71
_lastQuery = new Query ( ) ;
71
72
_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
+ }
74
78
75
79
Settings = Ioc . Default . GetRequiredService < Settings > ( ) ;
76
80
Settings . PropertyChanged += ( _ , args ) =>
@@ -1240,7 +1244,10 @@ private static List<Result> GetHistoryItems(IEnumerable<HistoryItem> historyItem
1240
1244
1241
1245
private async Task QueryResultsAsync ( bool searchDelay , bool isReQuery = false , bool reSelect = true )
1242
1246
{
1243
- _updateSource ? . Cancel ( ) ;
1247
+ lock ( _updateSourceLock )
1248
+ {
1249
+ _updateSource . Cancel ( ) ;
1250
+ }
1244
1251
1245
1252
App . API . LogDebug ( ClassName , $ "Start query with text: <{ QueryText } >") ;
1246
1253
@@ -1268,9 +1275,12 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
1268
1275
1269
1276
var isHomeQuery = query . RawQuery == string . Empty ;
1270
1277
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
+ }
1274
1284
1275
1285
ProgressBarVisibility = Visibility . Hidden ;
1276
1286
_isQueryRunning = true ;
@@ -1888,7 +1898,10 @@ protected virtual void Dispose(bool disposing)
1888
1898
{
1889
1899
if ( disposing )
1890
1900
{
1891
- _updateSource ? . Dispose ( ) ;
1901
+ lock ( _updateSourceLock )
1902
+ {
1903
+ _updateSource ? . Dispose ( ) ;
1904
+ }
1892
1905
_resultsUpdateChannelWriter ? . Complete ( ) ;
1893
1906
if ( _resultsViewUpdateTask ? . IsCompleted == true )
1894
1907
{
0 commit comments