-3

Possible Duplicate:
Why does Thread.sleep() behave in this way

This is a simple code that i have written:

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = "first";
        Thread.Sleep(1000);
        label1.Text = "second";
    }

When this code is executed and button is clicked, label1 displays text as only 'second' but not 'first'. I checked using break point, and statement label1.text="first" is executed but label does not display 'first'. Why is this so?

Community
  • 1
  • 1
Shiridish
  • 4,942
  • 5
  • 33
  • 64

4 Answers4

3

Because your code is running on the UI thread.

When your app is running and you're not clicking on things, the application's main thread is rushing around, waiting for events, processing Windows messages, and painting the screen. When you click your button, the app stops doing those things, and runs your function instead. While it's running your method, it can't paint the screen. Your function sets the label to "First", blocks the thread for a second, then sets the label to "second".

When you return, control passes back to the application, which is then free to repaint the form. It repaints it using the "second" label. The message loop never had chance to paint it with the "first" label.

TarkaDaal
  • 18,798
  • 7
  • 34
  • 51
3

You're running a single threaded application. Although the statement setting the label1 object's Text property to "first" has been executed, because you are causing the main application thread to pause, the "Windows Message Pump" is not being executed.

Dylan
  • 317
  • 1
  • 8
1

The problem is your a freezing the UI thread here.

The text "first" is set but then you put the main GUI thread to sleep - so the UI does not get painted.

After the sleep phase is over, the text "second" is set and the UI gets painted.

That's just the way Windows works, so you have to do that differently. e.g. using Threads, or Backgroundworker

Wolfgang Ziegler
  • 1,675
  • 11
  • 23
  • 1
    This isn't actually what happens. The message pump is still working, but it is busy handling the button click. The paint message occurs after the method returns. – Adam Houldsworth Jul 20 '12 at 10:29
  • Of course this is what happens. As soon as you call Sleep on the UI thread (i.e.inside the message pump) your application basically freezes (concerning the UI thread) and will not be repainted. – Wolfgang Ziegler Jul 20 '12 at 10:47
  • 1
    The sleep call *slows down* the execution of the method, but it is not responsible for the lack of repaint, it is just responsible for the lack of repaint being *noticeable*. The message pump is actually alive, it is busy processing the event handler. You seem to imply that the sleep call stops the "first" from being painted, but even without a sleep call "first" would never be painted, because the paint call onto the label would occur after this method exits, when the label state is "second". – Adam Houldsworth Jul 20 '12 at 10:51
0

If you want delay use timers.

The reason why you don't see the result in UI is because Dipatcher's priorities. The Render task has lower priority than the execution.

This helps you: TIMERS!

DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//your code goes here
}

If you work in WPF you can try this:

private void button1_Click(object sender, RoutedEventArgs e)
{
    label1.Text = "first";
    Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);
    Thread.Sleep(1000);                            
    label1.Text = "second";
}
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474