@@ -40,6 +40,7 @@ public partial class RichSuggestBox : ItemsControl
40
40
private const string PartHeaderContentPresenter = "HeaderContentPresenter" ;
41
41
private const string PartDescriptionPresenter = "DescriptionPresenter" ;
42
42
43
+ private readonly object _tokensLock ;
43
44
private readonly Dictionary < string , RichSuggestToken > _tokens ;
44
45
private readonly ObservableCollection < RichSuggestToken > _visibleTokens ;
45
46
@@ -61,10 +62,10 @@ public partial class RichSuggestBox : ItemsControl
61
62
/// </summary>
62
63
public RichSuggestBox ( )
63
64
{
65
+ _tokensLock = new object ( ) ;
64
66
_tokens = new Dictionary < string , RichSuggestToken > ( ) ;
65
67
_visibleTokens = new ObservableCollection < RichSuggestToken > ( ) ;
66
68
Tokens = new ReadOnlyObservableCollection < RichSuggestToken > ( _visibleTokens ) ;
67
- LockObj = new object ( ) ;
68
69
69
70
DefaultStyleKey = typeof ( RichSuggestBox ) ;
70
71
@@ -81,7 +82,7 @@ public RichSuggestBox()
81
82
public void ClearUndoRedoSuggestionHistory ( )
82
83
{
83
84
TextDocument . ClearUndoRedoHistory ( ) ;
84
- lock ( LockObj )
85
+ lock ( _tokensLock )
85
86
{
86
87
if ( _tokens . Count == 0 )
87
88
{
@@ -108,7 +109,7 @@ public bool TryGetTokenFromRange(ITextRange range, out RichSuggestToken token)
108
109
range = range . GetClone ( ) ;
109
110
if ( range != null && ! string . IsNullOrEmpty ( range . Link ) )
110
111
{
111
- lock ( LockObj )
112
+ lock ( _tokensLock )
112
113
{
113
114
return _tokens . TryGetValue ( range . Link , out token ) ;
114
115
}
@@ -272,7 +273,7 @@ private void RichEditBox_SelectionChanging(RichEditBox sender, RichEditBoxSelect
272
273
273
274
var range = selection . GetClone ( ) ;
274
275
range . Expand ( TextRangeUnit . Link ) ;
275
- lock ( LockObj )
276
+ lock ( _tokensLock )
276
277
{
277
278
if ( ! _tokens . ContainsKey ( range . Link ) )
278
279
{
@@ -580,7 +581,7 @@ internal async Task CommitSuggestionAsync(object selectedItem)
580
581
return ;
581
582
}
582
583
583
- lock ( LockObj )
584
+ lock ( _tokensLock )
584
585
{
585
586
var displayText = prefix + text ;
586
587
@@ -634,7 +635,7 @@ private bool TryCommitSuggestionIntoDocument(ITextRange range, string displayTex
634
635
635
636
private void ValidateTokensInDocument ( )
636
637
{
637
- lock ( LockObj )
638
+ lock ( _tokensLock )
638
639
{
639
640
foreach ( var ( _, token ) in _tokens )
640
641
{
@@ -655,7 +656,7 @@ private void ValidateTokenFromRange(ITextRange range)
655
656
// Check for duplicate tokens. This can happen if the user copies and pastes the token multiple times.
656
657
if ( token . Active && token . RangeStart != range . StartPosition && token . RangeEnd != range . EndPosition )
657
658
{
658
- lock ( LockObj )
659
+ lock ( _tokensLock )
659
660
{
660
661
var guid = Guid . NewGuid ( ) ;
661
662
if ( TryCommitSuggestionIntoDocument ( range , token . DisplayText , guid , CreateTokenFormat ( range ) , false ) )
@@ -919,7 +920,7 @@ private ITextCharacterFormat CreateTokenFormat(ITextRange range)
919
920
920
921
private void UpdateVisibleTokenList ( )
921
922
{
922
- lock ( LockObj )
923
+ lock ( _tokensLock )
923
924
{
924
925
var toBeRemoved = _visibleTokens . Where ( x => ! x . Active ) . ToArray ( ) ;
925
926
0 commit comments