Things developers face while programming: We have seen rapid clicking on a button invoking the same event or the same process many times. Here we will find a way to control the rapid click error of a button.

We can write a class named SingleClick, in which we will write a method, Click with Object and EventArgs, as arguments to do the magic. Refer to the below code snippet for more.

  1. public class SingleClick
  2. {
  3. bool hasClicked;
  4. #region Button Click
  5. Action < object, EventArgs > _setClick;
  6. public SingleClick(Action < object, EventArgs > setClick)
  7. {
  8. _setClick = setClick;
  9. }#endregion Button Click
  10. public void Click(object s, EventArgs e)
  11. {
  12. if (!hasClicked)
  13. {
  14. _setClick(s, e);
  15. hasClicked = true;
  16. }
  17. Reset();
  18. }
  19. async void Reset()
  20. {
  21. await Task.Delay(800);
  22. await Task.Run(new Action(() => hasClicked = false));
  23. }
  24. }
Usage :
  1. var button = new Button();
  2. button.Clicked += new SingleClick(Button_Clicked).Click;
Try this workaround and give me your valuable suggestions. Happy Coding.