G
Guest
Say we have a Form F, containing a UserControl Uc, which does some
action on a WindowsTimer callback. If F also contains a timer and this
timer has an interval shorter than its callback latency, then
Uc.Dispose() will cause an infinite loop.
The attached sample ilustrates the problem. Pressing a button
toggles between creating/destroying a UserControl. This works fine as
long as the form's timer callback is kept under <100 ms (the form's
timer interval). The attached sample uses 200ms and locks up after
pressing the create/destroy button a couple of times.
Any suggestions for workarounds? TIA.
-------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication4
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public UserControl1()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.BackColor = System.Drawing.Color.RosyBrown;
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(104, 24);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void timer1_Tick(object sender, System.EventArgs e)
{}
}
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.IContainer components;
[STAThread]
public static void Main()
{
Application.Run(new Form2());
}
public Form2()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.button1.Location = new System.Drawing.Point(112, 192);
this.button1.Text = "Ok";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Controls.Add(this.button1);
this.ResumeLayout(false);
}
UserControl1 myUserControl;
private void button1_Click(object sender, System.EventArgs e)
{
if (myUserControl == null)
{
myUserControl = new UserControl1();
myUserControl.Parent = this;
}
else
{
myUserControl.Dispose();
myUserControl = null;
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(200);
}
}
}
action on a WindowsTimer callback. If F also contains a timer and this
timer has an interval shorter than its callback latency, then
Uc.Dispose() will cause an infinite loop.
The attached sample ilustrates the problem. Pressing a button
toggles between creating/destroying a UserControl. This works fine as
long as the form's timer callback is kept under <100 ms (the form's
timer interval). The attached sample uses 200ms and locks up after
pressing the create/destroy button a couple of times.
Any suggestions for workarounds? TIA.
-------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication4
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public UserControl1()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.BackColor = System.Drawing.Color.RosyBrown;
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(104, 24);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void timer1_Tick(object sender, System.EventArgs e)
{}
}
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.IContainer components;
[STAThread]
public static void Main()
{
Application.Run(new Form2());
}
public Form2()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.button1.Location = new System.Drawing.Point(112, 192);
this.button1.Text = "Ok";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Controls.Add(this.button1);
this.ResumeLayout(false);
}
UserControl1 myUserControl;
private void button1_Click(object sender, System.EventArgs e)
{
if (myUserControl == null)
{
myUserControl = new UserControl1();
myUserControl.Parent = this;
}
else
{
myUserControl.Dispose();
myUserControl = null;
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(200);
}
}
}