Search code examples
c#wpfvisibilityinotifypropertychangedtextblock

Trying to display a "please wait" message in WPF during a long-running operation


I have a please wait TextBlock in my XAML whose Visibility property is bound to an IsBusy property on my ViewModel (which implements INotifyPropertyChanged). A separate button launches a long-running (2-3 seconds) process, like this:

IsBusy = true;

try
{
    // Do some long-running process
}

finally
{
    IsBusy = false;
}

But the please wait message never appears. I'm assuming the operation is running on the UI thread and therefore not giving it a chance to refresh? It does work if I run the above code on a separate thread, but I don't want the user to do anything while the operation is running - I'm happy for the UI to freeze.

How can I display the please wait message? Or would I be better running it on a background thread and (somehow) locking the UI for the duration? I'm using .Net 4 btw.


Solution

  • You're correct, it's not updating because you're running the code on the same thread, so rendering doesn't continue until after your process has finished.

    You said you aren't using a background thread because you don't want the user to do anything while the operation is running, but it's never a good idea to completely lock up your application like this.

    A better solution would be to bind the IsEnabled of your Form/Window/UserControl/etc to the IsBusy property. Then when it becomes busy, the entire form gets disabled to prevent the user from modifying it, while not locking up your application.