Button single click problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have been creating an Button using .Net C#.
Once the user clicks the button, the click event handler will perform task A. This normally takes around 2 minutes
During this 2 minutes, the user must not be allowed to click this button again

However, once the user clicks multiple times on this button, the click event handler will responds automatically
(i.e. during this 2 minutes, the user clicks 2 times. Then a total of 3 task A will be performed
It is acceptable if the user click the button after Task A completed

How do we prevent the latching of the click event
Please advise
Thanks a lo

Regards
Pat
 
Two obvious solutions would be:
1) Disable the button as soon as the event handling function is called (have
button.enabled=false) right at the beginning there).
2) Have a boolean static variable in the function (or a class level private
member) that tells you whether the function has been called already, and if
that boolean is true, leave the function without doing anything (you set the
boolean at the beginning of the function, and set it back in the end).

Another option would be to use locking of some sort (semaphore or mutex),
but it doesn't sound like you really need that.

I'd go for one - it's the simplest and makes it most obvious to the user
that he/she is not allowed to click the button again. You can even have a
tooltip that explains it, on the button.

--itai

Pat said:
Hi,

I have been creating an Button using .Net C#.
Once the user clicks the button, the click event handler will perform task
A. This normally takes around 2 minutes.
During this 2 minutes, the user must not be allowed to click this button again.

However, once the user clicks multiple times on this button, the click
event handler will responds automatically.
 
Hi,
I have tried out the first method and it doesn't work at all. Below is
the Click event handler routine of a normal button. It can be noticed
that if the user click a few times, when the button is disabled, the
"Hello" will be printed out multiple times.

private void button1_Click(object sender, System.EventArgs e) {
this.button1.Enabled = false;

Thread.Sleep(2000);
Console.WriteLine("Hello");
Thread.Sleep(1000);

this.button1.Enabled = true;
}

I have also try out the second method, but it still unable to work
properly cos the user's clicks still store into some kind of buffer.

Is there any way to resolve this issue?
Please help and advice.

Thanks a lot

Regards,
Patrick Foo (Pat)
 
do it from javascript instead so that the click won't have time to get to
the server
 
Sorry, I thought you were referring to a windows app.

In ASP (web) your solution would be to plant a javascript event right after
the submission of the form, that will disable the button before the page is
submitted (onClick="this.disabled=true").


--itai
 
Back
Top