I have an application in WPF which constantly changes the value(Updates!) in the GUI screen. This causes the memory usuage to go up. Especially #of Bytes in all heaps. The private bytes also increase. To simulate a similar scenario updating the GUI all the time, I created a simple application with following code.
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();
}
}
}
The XAML is
<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>
Now, I am unable to run this code(Edit: Code changed such that it compiles and runs fine, but memory problem is still there, I reduced the sleep time such that it is more visible) . What could be the reason.
Thanks.
Data class to store information (must implement INotifyPropertyChanged to update UI the changes). This solved the problem of memory that increased forever.