Mysterious behaviour (freezing)
-
:confused::confused::confused: If somebody could make sense of the behaviour of the following code, I'd be really happy. On the second line, either #define CRASH to have it crash or don't to have it, well, not crash. Then just pick the menu items sequentially from 1 to 4. The only difference is that in the crashing version, a UserControl is added as a control to a Panel instead of directly to a Form... After closing the form created in step 4, the application freezes, sometimes terminates with an ObjectDisposedException ... Cheers, Gunther :omg: ------------- CODE STARTS HERE ------------- // define "CRASH" to have it crash when you go from 1 to 4 ... #define CRASH using System; namespace TestApp { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { [STAThread] static void Main() { System.Windows.Forms.Application.Run(new Form1()); } private System.Windows.Forms.Panel panel1; public Form1() { this.panel1 = new System.Windows.Forms.Panel(); this.SuspendLayout(); // // Menu // this.Menu = new System.Windows.Forms.MainMenu(); System.Windows.Forms.MenuItem mi = new System.Windows.Forms.MenuItem("Crashing!?"); this.Menu.MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("1. Create Control"); mi.Click += new EventHandler(this.Create_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("2. Display Dialog"); mi.Click += new EventHandler(this.Dialog_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("3. Destroy Control"); mi.Click += new EventHandler(this.Destroy_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("4. Display Dialog"); mi.Click += new EventHandler(this.Dialog_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("-"); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("Quit"); mi.Click += new EventHandler(this.Quit_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); // // panel1 // this.panel1.AutoScroll = true; this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Siz
-
:confused::confused::confused: If somebody could make sense of the behaviour of the following code, I'd be really happy. On the second line, either #define CRASH to have it crash or don't to have it, well, not crash. Then just pick the menu items sequentially from 1 to 4. The only difference is that in the crashing version, a UserControl is added as a control to a Panel instead of directly to a Form... After closing the form created in step 4, the application freezes, sometimes terminates with an ObjectDisposedException ... Cheers, Gunther :omg: ------------- CODE STARTS HERE ------------- // define "CRASH" to have it crash when you go from 1 to 4 ... #define CRASH using System; namespace TestApp { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { [STAThread] static void Main() { System.Windows.Forms.Application.Run(new Form1()); } private System.Windows.Forms.Panel panel1; public Form1() { this.panel1 = new System.Windows.Forms.Panel(); this.SuspendLayout(); // // Menu // this.Menu = new System.Windows.Forms.MainMenu(); System.Windows.Forms.MenuItem mi = new System.Windows.Forms.MenuItem("Crashing!?"); this.Menu.MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("1. Create Control"); mi.Click += new EventHandler(this.Create_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("2. Display Dialog"); mi.Click += new EventHandler(this.Dialog_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("3. Destroy Control"); mi.Click += new EventHandler(this.Destroy_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("4. Display Dialog"); mi.Click += new EventHandler(this.Dialog_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("-"); this.Menu.MenuItems[0].MenuItems.Add(mi); mi = new System.Windows.Forms.MenuItem("Quit"); mi.Click += new EventHandler(this.Quit_Click); this.Menu.MenuItems[0].MenuItems.Add(mi); // // panel1 // this.panel1.AutoScroll = true; this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Siz
Reason, I cannot give, but fix I can: Replace: if (this.panel1.Controls[i].GetType() == typeof(System.Windows.Forms.UserControl)) this.Controls.RemoveAt(i); With: if (this.panel1.Controls[i].GetType() == typeof(System.Windows.Forms.UserControl)) { this.panel1.Controls[i].Controls.Clear(); this.panel1.Controls.RemoveAt(i); } Ran into this a while back....
-
Reason, I cannot give, but fix I can: Replace: if (this.panel1.Controls[i].GetType() == typeof(System.Windows.Forms.UserControl)) this.Controls.RemoveAt(i); With: if (this.panel1.Controls[i].GetType() == typeof(System.Windows.Forms.UserControl)) { this.panel1.Controls[i].Controls.Clear(); this.panel1.Controls.RemoveAt(i); } Ran into this a while back....
Thanks je, your suggestions works. However, meanwhile I also found that simply calling a Dispose() instead of the RemoveAt() also works. I also realized that my original problem was twofold - the one from the posted source, the other, giving the DisposeException only occurs when a LinkLabel control was part of a GroupBox on a UserControl ... Well, I just avoid having those in GroupBoxes for now ... Regards, Gunther