Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf: Fix performance setting TextBox.Text rapidly #2534

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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