title: How to update UI before (and after) long running UI process in WPF?
body: <p>I apologize if this is a duplicate, but I have not been able to find a question with a similar situation. If this is a duplicate please provide a link.</p>
  
  <p>I would like to show a "Loading..." overlay in my WPF application, when I am dynamically creating a lot of tabs. The overlay visibility is bound to a property called "ShowIsLoadingOverlay". However, the overlay is never shown. </p>
  
  <p>Due to the fact that the tabs are visual elements I can't move the creation into a BackgroundWorker.</p>
  
  <p>I have created a small prototype trying to explain the situation. This is the xaml:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication5.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Label HorizontalAlignment="Center" VerticalAlignment="Center" 
                 Visibility="{Binding ShowIsLoadingOverlay, Converter={StaticResource BooleanToVisibilityConverter}}" 
                 Content="Loading..." /&gt;
  
          &lt;Button Grid.Row="1" Content="Load" Click="Button_Click" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And this is the code behind:</p>
  
  <pre><code>public partial class MainWindow : Window, INotifyPropertyChanged
  {
     private bool m_ShowIsLoadingOverlay;
     public bool ShowIsLoadingOverlay
     {
        get
        {
           return m_ShowIsLoadingOverlay;
        }
        set
        {
           if ( m_ShowIsLoadingOverlay == value )
           {
              return;
           }
  
           m_ShowIsLoadingOverlay = value;
           NotifyPropertyChanged( "ShowIsLoadingOverlay" );
        }
     }
  
     public MainWindow()
     {
        InitializeComponent();
  
        DataContext = this;
     }
  
     private void Button_Click( object sender, RoutedEventArgs e )
     {
        ShowIsLoadingOverlay = true;
  
        CreateTabs();
  
        ShowIsLoadingOverlay = false;
     }
  
     private void CreateTabs()
     {
        // Simulate long running process to create tabs
        Thread.Sleep( 3000 );
     }
  
     // Implementation of INotifyPropertyChanged has been left out.
  }
  </code></pre>
  
  <p>The problem is that the overlay is never shown. I know that it has something to do with the UI not updated correctly before and after the ShowIsLoadingOverlay property has changed. And I believe it also has something to do with the lack of using the dispatcher.</p>
  
  <p>I have tried many, many combinations of Dispatcher.Invoke, Dispatcher.BeginInvoke surrounding when changing the property and/or surrounding the CreateTabs call. And I have tried changing the DispatcherPriority to "force" the overlay to show before starting to create the tabs. But I just can't make it work...</p>
  
  <p>Could you please tell me how to accomplish this task? And more importantly; provide an explanation, because I do not get this.</p>
  
  <p>In advance,
  thank you.</p>
  
  <p>Best regards,
  Casper Korshøj</p>