locked
This is odd? RRS feed

  • Question

  • Hi everyone,

     This is kinda odd: I have a simple application, just a textblock called txtMain and a button. The following code just displays "Bye!", not "Hello!". Same thing if i switch out the Thread.Sleep for a FOR loop. Anyone know why?

    private void btnCLick_Click(object sender, RoutedEventArgs e)

    {

    txtMain.Text =
    "Hello!";
    System.Threading.
    Thread.Sleep(1000);
    txtMain.Text = "bye!";

    }

     

     // Thanks, MightyBob

    Monday, June 1, 2009 4:05 AM

Answers

  • No that's not the case. We are living in an area where not everything get's reflected immediately. Specially because you block the UI-Thread. That's a very bad thing to do, specially in the browser area. Please do the waiting in another thread, and call back to the ui-thread when necessary. Getting back to the UI Thread
    Monday, June 1, 2009 4:35 AM

All replies

  • The UI thread never got the chance to display "Hello!".  While you are sleeping the current thread is blocked and when you are back, the Text has been changed to "bye!".  By the time the UI thread gets the chance to update, it only sees "bye!".

    You can do something like this: display "Hello", then schedule an event to display "bye!" after 1 second.

    Monday, June 1, 2009 4:15 AM
  • Ok, but since I'm setting the .text property before I'm putting the thread to sleep, should it not set it and THEN go to sleep? Just curious.

    Monday, June 1, 2009 4:25 AM
  • No that's not the case. We are living in an area where not everything get's reflected immediately. Specially because you block the UI-Thread. That's a very bad thing to do, specially in the browser area. Please do the waiting in another thread, and call back to the ui-thread when necessary. Getting back to the UI Thread
    Monday, June 1, 2009 4:35 AM