WPF窗口不断更新GUI

时间:2012-10-11 03:23:31

标签: multithreading memory-leaks textbox updates

我在WPF中有一个应用程序,它不断更改GUI屏幕中的值(更新!)。这会导致内存使用率上升。尤其是所有堆中的字节#of。专用字节也增加了。为了模拟类似的场景一直在更新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>

现在,我无法运行此代码(编辑:代码已更改,以便编译并运行正常,但内存问题仍然存在,我减少了睡眠时间,使其更加明显)。可能是什么原因。

感谢。

1 个答案:

答案 0 :(得分:0)

用于存储信息的数据类(必须实现INotifyPropertyChanged以更新UI的更改)。这解决了永远增加的记忆问题。