G
Guest
While testing one of our applications, I found some interesting behavior with
the Button click event using .NET 2.0.
Let's say we have a windows form with a button on it. When the user clicks
on the button, the button disables itself (to prevent the user from clicking
again), does some process that takes some time to complete, and then enables
the button again.
The behavior I'm seeing is when a user clicks on the button, the button does
disable. And while the button is still disabled, if the user clicks on the
button again many times, the clicks "get queued up" and when the process
finishes the button enables and then very quickly disables itself and start
the process again.
Depending on how many times you click and how fast, the process will repeat
itself several times. I don't think this should be happening. The MSDN
documentation says that when a control is disabled, the events should also be
disabled. So why would a click event be firing when a button is disabled?
I created a small test program that re-creates the issue. It's a form with
a button, a status strip and a counter. The button simulates some long
process and increments the counter and updates the status strip. If you
click on the button many times when it is disabled, the click events get
queued up and the button "get's clicked" again several times after the first
process is over.
namespace CSharpTestWinApp
{
public partial class Form1 : Form
{
private int m_counter;
public Form1()
{
InitializeComponent();
this.m_counter = 0;
this.m_statusLabel.Text = this.m_counter.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.button1.Enabled = false;
this.m_counter++;
this.m_statusLabel.Text = this.m_counter.ToString();
Application.DoEvents();
System.Threading.Thread.Sleep(5000);
}
finally
{
this.button1.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.m_counter = 0;
this.m_statusLabel.Text = this.m_counter.ToString();
}
}
}
the Button click event using .NET 2.0.
Let's say we have a windows form with a button on it. When the user clicks
on the button, the button disables itself (to prevent the user from clicking
again), does some process that takes some time to complete, and then enables
the button again.
The behavior I'm seeing is when a user clicks on the button, the button does
disable. And while the button is still disabled, if the user clicks on the
button again many times, the clicks "get queued up" and when the process
finishes the button enables and then very quickly disables itself and start
the process again.
Depending on how many times you click and how fast, the process will repeat
itself several times. I don't think this should be happening. The MSDN
documentation says that when a control is disabled, the events should also be
disabled. So why would a click event be firing when a button is disabled?
I created a small test program that re-creates the issue. It's a form with
a button, a status strip and a counter. The button simulates some long
process and increments the counter and updates the status strip. If you
click on the button many times when it is disabled, the click events get
queued up and the button "get's clicked" again several times after the first
process is over.
namespace CSharpTestWinApp
{
public partial class Form1 : Form
{
private int m_counter;
public Form1()
{
InitializeComponent();
this.m_counter = 0;
this.m_statusLabel.Text = this.m_counter.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.button1.Enabled = false;
this.m_counter++;
this.m_statusLabel.Text = this.m_counter.ToString();
Application.DoEvents();
System.Threading.Thread.Sleep(5000);
}
finally
{
this.button1.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.m_counter = 0;
this.m_statusLabel.Text = this.m_counter.ToString();
}
}
}