0

GUI画面で値を常に変更するWPFのアプリケーションがあります(更新!)。これにより、メモリ使用量が増加します。特に、すべてのヒープのバイト数。プライベート バイトも増加します。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>

今、私はこのコードを実行できません (編集: コンパイルして正常に実行されるようにコードを変更しましたが、メモリの問題はまだあります。より見やすくするためにスリープ時間を短縮しました)。その理由は何でしょうか。

ありがとう。

4

1 に答える 1

0

情報を格納するためのデータ クラス (UI の変更を更新するには、INotifyPropertyChanged を実装する必要があります)。これにより、永遠に増加するメモリの問題が解決されました。

于 2012-10-16T00:32:24.343 に答える