Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:30:36+00:00 2026-05-27T14:30:36+00:00

I’ve got a WinForm, which contains a button. In the button’s click event, I

  • 0

I’ve got a WinForm, which contains a button. In the button’s click event, I call button1.Enabled = false and then a number of lines of code follow. After this code, I call button1.Enabled = true.

This is done so that the button cannot be pressed while the execution is being carried out. The strange thing is that while the button is disabled, if the user clicks on the button many times, the button is actually clicked.

Could anyone explain why this is happening? The code looks like this:

button1.Enabled = false
//Code
//Code
//Code
button1.Enabled = true
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:30:37+00:00Added an answer on May 27, 2026 at 2:30 pm

    My guess is that you’re actually doing a load of work in those lines of code, blocking the UI thread. You shouldn’t do that – you should set the button to be disabled, then start off your work in a background thread (or using asynchronous calls, if most of the time is spent talking to a web service or something similar). Then when the work is finished, it should marshal a call back into the UI thread to re-enable the button.

    (If that’s not the problem, please show a short but complete program which demonstrates the problem.)

    EDIT: Thanks to Sarwar’s comments, I understand why you’re getting the multiple events being fired – they’re being queued up (due to the UI thread being blocked), and only processed after the button is re-enabled. So yes, if you avoid doing lots of work on the UI thread, it can handle the clicks on the disabled button, ignore them, and then there won’t be a backlog when the button is re-enabled.

    EDIT: For anyone who is interested in trying this themselves, here’s my test program demonstrating the problem:

    using System;
    using System.Windows.Forms;
    using System.Threading;
    using System.Threading.Tasks; // For the fix below
    
    class Program
    {
        static void Main()
        {
            Button button = new Button { Text = "Click me" };
            button.Click += HandleClick;
            Form form = new Form { Controls = { button } };
            Application.Run(form);
        }
    
        static void HandleClick(object sender, EventArgs e)
        {
            Console.WriteLine("In click handler");
            Button button = (Button) sender;
            button.Enabled = false;
            Thread.Sleep(10000);
            button.Enabled = true;        
            Console.WriteLine("Finishing click handler");
        }
    }
    

    And the fix using C# 5’s async handling:

    static async void HandleClick(object sender, EventArgs e)
    {
        Console.WriteLine("In click handler");
        Button button = (Button) sender;
        button.Enabled = false;
        // Delay by 10 seconds, but without blocking the UI thread
        await TaskEx.Delay(10000);
        button.Enabled = true;        
        Console.WriteLine("Finishing click handler");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.