Repeated Adding,Removing control from form

  • Thread starter Thread starter Brad Huff
  • Start date Start date
B

Brad Huff

I have included a snippet below which illustrates the
problem with adding and removing controls in a repeating
fashion. At least on my computer (Windows XP) the
program crashes after several thousand iterations. Is
there a way to correct this simple program so it doesn't
crash or is there a bug with .NET 1.1?

Thanks, Brad


using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using CustomDialogs;
using CustomControls;
using System.Threading;

public class Test{

public class MyForm:Form{
XButton button = new XButton();
XButton test = new XButton();
int count;

public MyForm(): base() {
InitializeComponent();
}
private void InitializeComponent(){
this.Width = 1000;
this.Height = 900;
this.Controls.Add(button);
button.Click +=
new EventHandler(click);
test.Location = new Point(300,300);
}
void click (object sender, EventArgs e){
for (int i = 0;i<30000;i++){
Controls.Add(test);
Controls.Remove(test);
count++;
button.Text = count.ToString();
button.Refresh();
}
}

}
public static void Main(string[] args)
{
MyForm form = new MyForm();
Application.Run(form);

}

}
 
I duplicated this and indeed it crashes at about
iteration no. 4735. There is no onbious error message, so
I just assume that the memory gets full and a process of
using virtual memory starts and for some reaon it is not
sucessfull... This is really weird... If i force the
application to shut down I also see the button changed
to "30000", which means that somehow the loop finishes...
 
What is also interesting is that the program will crash
at any spot, the iteration crashing at just about any
time.

Brad Huff
-----Original Message-----

I duplicated this and indeed it crashes at about
iteration no. 4735. There is no onbious error message, so
I just assume that the memory gets full and a process of
using virtual memory starts and for some reaon it is not
sucessfull... This is really weird... If i force the
application to shut down I also see the button changed
to "30000", which means that somehow the loop finishes...
-----Original Message-----
I have included a snippet below which illustrates the
problem with adding and removing controls in a repeating
fashion. At least on my computer (Windows XP) the
program crashes after several thousand iterations. Is
there a way to correct this simple program so it doesn't
crash or is there a bug with .NET 1.1?

Thanks, Brad


using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using CustomDialogs;
using CustomControls;
using System.Threading;

public class Test{

public class MyForm:Form{
XButton button = new XButton();
XButton test = new XButton();
int count;

public MyForm(): base() {
InitializeComponent();
}
private void InitializeComponent(){
this.Width = 1000;
this.Height = 900;
this.Controls.Add(button);
button.Click +=
new EventHandler(click);
test.Location = new Point(300,300);
}
void click (object sender, EventArgs e){
for (int i = 0;i<30000;i++){
Controls.Add(test);
Controls.Remove(test);
count++;
button.Text = count.ToString();
button.Refresh();
}
}

}
public static void Main(string[] args)
{
MyForm form = new MyForm();
Application.Run(form);

}

}
.
.
 
I think the message queue is being overrun with unprocessed messages in this
snippet. Adding a DoEvents call allowed it to finish the 30000 iterations
for me.

Controls.Add(test);
Application.DoEvents();
Controls.Remove(test);

========================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools
 
Back
Top