Skip to content

Commit 17ed69e

Browse files
Avoid queuing not-needed AP updates (#4024)
If an update has already been queued for processing (but not yet completed), simply skip queuing another update.
1 parent bde4374 commit 17ed69e

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/MaterialDesignThemes.Wpf/Behaviors/TextBoxLineCountBehavior.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows.Threading;
1+
using System.Threading;
2+
using System.Windows.Threading;
23
using Microsoft.Xaml.Behaviors;
34

45
namespace MaterialDesignThemes.Wpf.Behaviors;
@@ -11,18 +12,22 @@ public class TextBoxLineCountBehavior : Behavior<TextBox>
1112
private void AssociatedObjectOnTextChanged(object sender, TextChangedEventArgs e) => UpdateAttachedProperties();
1213
private void AssociatedObjectOnLayoutUpdated(object? sender, EventArgs e) => UpdateAttachedProperties();
1314

15+
private int _uiUpdateInProgress = 0;
16+
1417
private void UpdateAttachedProperties()
1518
{
16-
if (AssociatedObject is { } associatedObject)
19+
if (AssociatedObject is { } associatedObject &&
20+
Interlocked.CompareExchange(ref _uiUpdateInProgress, 1, 0) == 0)
1721
{
1822
associatedObject.Dispatcher
19-
.BeginInvoke(() =>
20-
{
21-
int lineCount = associatedObject.LineCount;
22-
associatedObject.SetCurrentValue(TextFieldAssist.TextBoxLineCountProperty, lineCount);
23-
associatedObject.SetCurrentValue(TextFieldAssist.TextBoxIsMultiLineProperty, lineCount > 1);
24-
},
25-
DispatcherPriority.Background);
23+
.BeginInvoke(() =>
24+
{
25+
int lineCount = associatedObject.LineCount;
26+
associatedObject.SetCurrentValue(TextFieldAssist.TextBoxLineCountProperty, lineCount);
27+
associatedObject.SetCurrentValue(TextFieldAssist.TextBoxIsMultiLineProperty, lineCount > 1);
28+
Interlocked.CompareExchange(ref _uiUpdateInProgress, 0, 1);
29+
},
30+
DispatcherPriority.Background);
2631
}
2732
}
2833

@@ -35,10 +40,10 @@ protected override void OnAttached()
3540

3641
protected override void OnDetaching()
3742
{
38-
if (AssociatedObject != null)
43+
if (AssociatedObject is { } associatedObject)
3944
{
40-
AssociatedObject.TextChanged -= AssociatedObjectOnTextChanged;
41-
AssociatedObject.LayoutUpdated -= AssociatedObjectOnLayoutUpdated;
45+
associatedObject.TextChanged -= AssociatedObjectOnTextChanged;
46+
associatedObject.LayoutUpdated -= AssociatedObjectOnLayoutUpdated;
4247
}
4348
base.OnDetaching();
4449
}

0 commit comments

Comments
 (0)