我在 WPF 中有一个应用程序,它不断更改 GUI 屏幕中的值(更新!)。这会导致内存使用量上升。特别是所有堆中的#of Bytes。私有字节也会增加。为了模拟始终更新 GUI 的类似场景,我使用以下代码创建了一个简单的应用程序。
namespace TextFieldUpdateTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
Int32 Counter = 0;
Thread NewThread;
private delegate void ForLoop();
public delegate void LoopHandler(object sender,EventArgs args);
public event LoopHandler LoopChange;
public Window1()
{
InitializeComponent();
NewThread = new Thread(new ThreadStart(ForLoop1));
LoopChange +=new LoopHandler(Window1_LoopChange);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (!NewThread.IsAlive)
{
NewThread.Start();
}
}
public void ForLoop1()
{
for (; ; )
{
LoopChange(0, EventArgs.Empty);
Thread.Sleep(10);
}
}
public void Window1_LoopChange(object sender,EventArgs args)
{
LoopChange -= this.Window1_LoopChange;
this.Dispatcher.Invoke(new ForLoop(ForLoop2));
LoopChange +=new LoopHandler(Window1_LoopChange);
}
public void ForLoop2()
{
Counter++;
if (Counter % 2 == 0)
{
textBox1.Text = "11111";
textBox2.Text = "22222";
textBox3.Text = "33333";
textBox4.Text = "44444";
textBox5.Text = "55555";
textBox6.Text = "66666";
}
else
{
textBox1.Text = "00000";
textBox2.Text = "77777";
textBox3.Text = "88888";
textBox4.Text = "99999";
textBox5.Text = "10101";
textBox6.Text = "45454";
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (NewThread.IsAlive) NewThread.Abort();
}
}
}
XAML 是
<Window x:Class="TextFieldUpdateTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="427">
<Grid>
<Label Height="30" HorizontalAlignment="Left" Margin="42,41,0,0" Name="label1" VerticalAlignment="Top" Width="79">1</Label>
<Label Height="30" HorizontalAlignment="Left" Margin="42,72,0,0" Name="label2" VerticalAlignment="Top" Width="79">2</Label>
<Label HorizontalAlignment="Left" Margin="42,101,0,131" Name="label3" Width="79">3</Label>
<Label HorizontalAlignment="Left" Margin="42,131,0,101" Name="label4" Width="79">4</Label>
<Label Height="30" HorizontalAlignment="Left" Margin="42,0,0,71" Name="label5" VerticalAlignment="Bottom" Width="79">5</Label>
<Label Height="30" HorizontalAlignment="Left" Margin="42,0,0,40" Name="label6" VerticalAlignment="Bottom" Width="79">6</Label>
<TextBox Height="23" Margin="131,43,155,0" Name="textBox1" VerticalAlignment="Top" FontSize="16" />
<TextBox Height="23" Margin="131,74,155,0" Name="textBox2" VerticalAlignment="Top" FontSize="16" />
<TextBox Height="23" Margin="131,103,155,0" Name="textBox3" VerticalAlignment="Top" FontSize="16"/>
<TextBox Height="23" Margin="131,0,155,106" Name="textBox4" VerticalAlignment="Bottom" FontSize="16" />
<TextBox Height="23" Margin="131,0,155,78" Name="textBox5" VerticalAlignment="Bottom" FontSize="16" />
<TextBox Height="23" Margin="131,0,155,47" Name="textBox6" VerticalAlignment="Bottom" FontSize="16" />
<Button Height="23" HorizontalAlignment="Right" Margin="0,41,22,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Button1</Button>
<Button Height="23" HorizontalAlignment="Right" Margin="0,101,22,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click">Button2</Button>
</Grid>
现在,我无法运行此代码(编辑:代码已更改,使其编译和运行正常,但内存问题仍然存在,我减少了睡眠时间,使其更明显)。可能是什么原因。
谢谢。