Disabled button calling even handler on button click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandeepkavade
    New Member
    • Mar 2007
    • 30

    #1

    Disabled button calling even handler on button click

    Hi
    I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled?

    here is the code snippet: (Refer the attachment for test application)

    Code:
    public partial  class Form1  : Form
        {
            int i = 0;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.button1.Enabled = false;
                System.Threading.Thread.Sleep(3000);
                i++;
                this.label1.Text = "Total Calls: " + i.ToString();
                this.button1.Enabled = true;
            }
        }
    when the button is disabled and you click on it, you can see the count gets increased.

    Please help
    Attached Files
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I believe it's because you've had the thread sleep so that any input on it is buffered and released when the thread is done sleeping.

    Try using a timer instead... set the interval to whatever you like (probably 3 seconds as you're doing with the sleep), then disable the button in the click call and start the timer. In the timer tick event handler, stop the timer and enable the button.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      You can also unsubscribe the button from the handler while it is disabled. That way even if the press event is stored it goes nowhere.

      Comment

      • ThatThatGuy
        Recognized Expert Contributor
        • Jul 2009
        • 453

        #4
        Originally posted by sandeepkavade
        Hi
        I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled?

        here is the code snippet: (Refer the attachment for test application)

        public partial class Form1 : Form
        {
        int i = 0;
        public Form1()
        {
        InitializeCompo nent();
        }

        private void button1_Click(o bject sender, EventArgs e)
        {
        this.button1.En abled = false;
        System.Threadin g.Thread.Sleep( 3000);
        i++;
        this.label1.Tex t = "Total Calls: " + i.ToString();
        this.button1.En abled = true;
        }
        }

        when the button is disabled and you click on it, you can see the count gets increased.

        Please help
        Try tlhintoq's suggestion of unsubscrbing from the event...

        e.g.
        Code:
        Button1.Click-=new MouseEventArgs(Button1_Click);

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          The only reason I'd still advocate using a timer instead of a Thread.Sleep (disable button or remove the handler, either doesn't matter) is so your form is still responsive for the duration of your operation, which should also be done in another thread (which sounds like it might be since you're doing a sleep).

          I say this because it's generally a bad thing when your form becomes unresponsive for whatever reason. It can be frustrating for the user and, in some cases, it can be dangerous if the application controls something important and control is locked out because the form is tied up doing a task.

          Obviously you'll do what's right for you, but it's something to think about :)

          Comment

          • justreader
            New Member
            • Apr 2014
            • 1

            #6
            If you call Appliction.DoEv ents() before enabling button app will work as you intended.

            Reason clicks get through is they pile up in messages queue, which is processed after your method ends, and button is enabled by that time. Processing messages queue before enabling button solves it.

            Comment

            Working...