私はWindows Forms開発者であり、現在WPFで忙しいです。テキストボックスのテキストレンダリングに関する2つの技術のパフォーマンス比較をすばやく行うために、私はウィンドウ内に膨大な数のテキストボックスを作成し、100msごとにテキストを更新する小さなプログラムを作成しました。多くのテキストボックスを使用したテキストのレンダリングが遅い
私の驚いたことに、テストアプリケーションのWPFバージョンは、WinFormsバージョンよりもはるかに低速です。ほとんどの場合、アプリケーションが応答しません。たとえば、ウィンドウのサイズを変更しようとしたときなどです。アプリケーションのWinFormsバージョンは円滑に実行されます。
私の質問は:WPFコントロールを使用する方法に問題がありますか(WPFのコントロールコンテナとしてWrapPanelを使用し、WinFormsのFlowLayoutPanelを使用します)。または、WinFormsよりも実際にテキストのレンダリングが遅いですか?
WPF:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace PerformanceTestWPF
{
public partial class MainWindow : Window
{
DispatcherTimer _timer = new DispatcherTimer();
Random _r = new Random();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 400; i++)
this.wrapPanel.Children.Add(new TextBox {Height = 23, Width = 120, Text = "TextBox"});
_timer.Interval = new TimeSpan(0,0,0,0, 100);
_timer.Tick += _timer_Tick;
_timer.Start();
}
private void _timer_Tick(object sender, EventArgs e)
{
foreach (var child in wrapPanel.Children)
{
var textBox = child as TextBox;
if (textBox != null)
{
textBox.Text = _r.Next(0, 1000).ToString();
}
}
}
}
}
リサイズ:
using System;
using System.Windows.Forms;
namespace PerformanceTestWinforms
{
public partial class Form1 : Form
{
Timer _timer = new Timer();
Random _r = new Random();
public Form1()
{
InitializeComponent();
for (int i = 0; i < 400; i++)
this.flowLayoutPanel1.Controls.Add(new TextBox { Height = 23, Width = 120, Text = "TextBox" });
_timer.Interval = 100;
_timer.Tick += _timer_Tick;
_timer.Start();
}
private void _timer_Tick(object sender, EventArgs e)
{
foreach (var child in flowLayoutPanel1.Controls)
{
var textBox = child as TextBox;
if (textBox != null)
{
textBox.Text = _r.Next(0, 1000).ToString();
}
}
}
}
}
結論にジャンプしません。高価なディスパッチャのサイクルかもしれません。私のマシンでプロファイリングができません(より高い資格情報が必要です)。どの機能が実際にボトルネックであるかを知るためにそれを実行できるかどうかを確認してください。 –