Bug report: Dispose for UserControl causes infinite loop

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);
}
}
}
 
J

Jeffrey Tan[MSFT]

Hi Ken,

I have reproduced our your issue, I will spend some time on this issue. I
will update you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Heehee,

Sorry for letting you wait for so long.

After doing some research, I found there is nothing wrong with your
application, so I think this maybe a bug of our product(just as you said in
title). So I have contacted our product team for this issue.

Now, I received a feedback from our product team to confirm this issue as a
bug, it has be added into product's internal database.

Thanks very much for your product feedback and sharing information with the
community!! Your feedback will make our product get better and better.
============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top