tags:

views:

62

answers:

3

The LoadIt() method below takes 5-10 seconds to complete. I want the message area to display "Loading..." before LoadIt() starts and display "Reloaded" after it finishes.

How can I do that?

The following code doesn't work. It seems to not update the label until everything is finished, at which point it just displays "Reloaded" again.

private void Button_Click(object sender, RoutedEventArgs e)
{
    lblMessage.Text = "Loading...";
    LoadIt();
    lblMessage.Text = "Reloaded";
}
+1  A: 

you could move LoadIt to a separate thread, or you could simulate the WinForms Application.DoEvents but this is quite a hack (http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!526.entry)

Martin Moser
+1  A: 

You can use the Dispatcher object to start tasks in the background thread:

public delegate void LoadItDelegate();

this.Dispatcher.BeginInvoke(
    System.Windows.Threading.DispatcherPriority.Background,
    new LoadItDelegate(LoadIt));

Make sure its in the background, because the UI thread has more priority, so the UI gets updated.. and also move your "I am done message" to the end of your LoadIt method :)

Arcturus
+1  A: 

There's more than one solution discussed here:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6fce9b7b-4a13-4c8d-8c3e-562667851baa/

Iain M Norman