Suppose you have a button on a form that counts to 1000 in a textbox and then clears it.
If I quickly click the button five times (in runtime) the Click event handler will be called 5 times and I will see the count to 1000 five times.
Is it possible to disable other clicks on that button while the first click is counting?
Note: Disabling the button in the first statement of the click handler and then re-enabling at the end does not work. Also, unsubscribing/subscribing to the click event (the -= followed by +=) does not work.
Here a sample to illustrate:
private bool runningExclusiveProcess = false;
private void button1_Click(object sender, EventArgs e)
{
this.button1.Click -= new System.EventHandler(this.button1_Click);
if (!runningExclusiveProcess)
{
runningExclusiveProcess = true;
button1.Enabled = false;
textBox1.Clear();
for (int i = 0; i < 1000; i++)
{
textBox1.AppendText(i + Environment.NewLine);
}
runningExclusiveProcess = false;
button1.Enabled = true;
}
this.button1.Click += new System.EventHandler(this.button1_Click);
}