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 7738255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:13:02+00:00 2026-06-01T08:13:02+00:00

I have the following code. Within the same block of code, I disable a

  • 0

I have the following code. Within the same block of code, I disable a control then enabled it, but it does not seem to work and remain the initial IsEnable state.

// Button never enable
public void Foo()
{
   button1.IsEnabled = false
   Thread.Sleep(3000);
   button1.IsEnabled = true;
   Thread.Sleep(3000);
   button1.IsEnabled = false;
}
  • 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-06-01T08:13:03+00:00Added an answer on June 1, 2026 at 8:13 am

    First of all: it isn’t that strange that the button is not enabled after the Foo method is done, as the last line disables the button.

    If you want to “see” the button’s enabled state change, you need to handle window messages before sleeping. Thread.Sleep causes the UI thread to sleep, so there will be no updates to the user interface. What you need to do is make the application handle all pending window messages.

    public static void DoEvents()
    {
        if (Application.Current != null)
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
    }
    
    public void Foo()
    {
       button1.IsEnabled = false
       DoEvents();
       Thread.Sleep(3000);
    
       button1.IsEnabled = true;
       DoEvents();
       Thread.Sleep(3000);
    
       button1.IsEnabled = false;
    }
    

    EDIT
    Further explanation:

    When the first Thread.Sleep is called, the button is disabled (IsEnabled is false). Then, after waiting for 3 seconds, you set the button to enabled, and after that to disable it again. The button is actually in that state, but there is no visual feedback (as in: if you look at the form, you won’t see the button “blink”).

    This is because you are blocking the UI thread by calling Thread.Sleep. This prevents the window from receiving window messages, which tell the window when to repaint itself or one of its children (the button doesn’t “know” it should be gray).

    So you need to give the window a chance to receive the messages and update its children. In Windows Forms applications, this was done with Application.DoEvents, which processed all pending window messages.

    There’s no such thing in WPF as Application.DoEvents, but the code I’ve posted emulates this by spwaning a Thread that does nothing – but that little time between changing the button’s enabled state and blocking the UI thread is enough for the button control to repaint itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, but i got a problem. Within my While, a
I have the following code within a stored procedure. WHERE WPP.ACCEPTED = 1 AND
Within one Activity I have th following piece of code: public void onStartMonitoringToggleClicked(View v)
In the following code, I have verified that connecting to a URL from within
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which recursively operates on each element within a List
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a

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.