Skip to content

Commit

Permalink
Merge pull request #2534 from cwensley/curtis/wpf-fix-slow-textbox-te…
Browse files Browse the repository at this point in the history
…xt-updates

Wpf: Fix performance setting TextBox.Text rapidly
  • Loading branch information
cwensley authored Jul 26, 2023
2 parents 193de2d + fa3fbce commit a7b303d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Eto.Wpf/Forms/Controls/TextBoxHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime;
using swd = System.Windows.Documents;
namespace Eto.Wpf.Forms.Controls
{
Expand All @@ -16,6 +17,14 @@ public class TextBoxHandler : TextBoxHandler<xwt.WatermarkTextBox, TextBox, Text
internal static object CurrentText_Key = new object();
internal static object CurrentSelection_Key = new object();
internal static object DisableTextChanged_Key = new object();
internal static object EnableNoGCRegion_Key = new object();

/// <summary>
/// Gets or sets the default value indicating to use GC.TryStartNoGCRegion() when setting TextBox.Text
/// to avoid performance issues with WPF.
/// See https://github.com/dotnet/wpf/issues/5887
/// </summary>
public static bool EnableNoGCRegionDefault = true;

protected override swc.TextBox TextBox => Control;

Expand Down Expand Up @@ -58,6 +67,18 @@ public override bool ShowBorder
}
}

/// <summary>
/// Gets or sets a value indicating to use GC.TryStartNoGCRegion() when setting TextBox.Text
/// to avoid performance issues with WPF.
/// See https://github.com/dotnet/wpf/issues/5887
/// </summary>
/// <seealso cref="TextBoxHandler.EnableNoGCRegionDefault" />
public bool EnableNoGCRegion
{
get => Widget.Properties.Get<bool?>(TextBoxHandler.EnableNoGCRegion_Key) ?? TextBoxHandler.EnableNoGCRegionDefault;
set => Widget.Properties.Set(TextBoxHandler.EnableNoGCRegion_Key, value);
}

public TextAlignment TextAlignment
{
get { return TextBox.TextAlignment.ToEto(); }
Expand Down Expand Up @@ -285,7 +306,17 @@ public string Text
if (args.Cancel)
return;
var needsTextChanged = TextBox.Text == newText;

// Improve performance when setting text often
// See https://github.com/dotnet/wpf/issues/5887#issuecomment-1604577981
if (EnableNoGCRegion)
GC.TryStartNoGCRegion(1000000); // is this magic number reasonable??

TextBox.Text = newText;

if (EnableNoGCRegion && GCSettings.LatencyMode == GCLatencyMode.NoGCRegion)
GC.EndNoGCRegion();

if (needsTextChanged)
{
Callback.OnTextChanged(Widget, EventArgs.Empty);
Expand Down
49 changes: 49 additions & 0 deletions test/Eto.Test/UnitTests/Forms/Controls/TextBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,55 @@ public void InsertingTextShouldFireTextChanging(string oldText, string newText,
Assert.AreEqual(text, args.Text, "#2.4");
Assert.AreEqual(Range.FromLength(rangeStart, rangeLength), args.Range, "#2.5");
}

[Test, ManualTest]
public void ManyUpdatesShouldNotCauseHangs()
{
TimeSpan maxElapsed = TimeSpan.MinValue;
ManualForm(
"There should not be any pausing",
form =>
{
var textBoxes = new List<T>();
var layout = new DynamicLayout();
for (int x = 0; x < 10; x++)
{
layout.BeginHorizontal();
for (int y = 0; y < 10; y++)
{
var textBox = new T();
textBoxes.Add(textBox);
layout.Add(textBox, true);
}
layout.EndHorizontal();
}
var sw = new Stopwatch();
var timer = new UITimer { Interval = 0.01 };
timer.Elapsed += (sender, e) =>
{
var elapsed = sw.Elapsed;
if (elapsed > maxElapsed)
{
maxElapsed = elapsed;
}
sw.Restart();
var rnd = new Random();
foreach (var tb in textBoxes)
{
tb.Text = rnd.Next(int.MaxValue).ToString();
}
};
form.Shown += (sender, e) =>
{
timer.Start();
sw.Start();
};
form.Closed += (sender, e) => timer.Stop();
return layout;
});
Assert.Less(maxElapsed, TimeSpan.FromSeconds(1), "There were long pauses in the UI");
}
}


Expand Down

0 comments on commit a7b303d

Please sign in to comment.