'Difference between Thread in WinForm and WPF

I have a Task that update UI in a window. But I don't understand behavior of Thread in WinForm and WPF.

In MainWindow I have two buttons, one that starts Task and one that opens a new window.

private void btnOpenWindow_Click(object sender, RoutedEventArgs e)
{
     var window = new Window();
     OpenedWindow = window;
     window.ShowDialog();
}

private void btnRunTask_Click(object sender, RoutedEventArgs e)
{
     Task.Run(() =>
     {
        //If I call UpdateOpenedWindow() with Dispatcher, CheckAccess() is true
        //this.Dispatcher.Invoke(() => UpdateOpendedWindow());
        UpdateOpenedWindow();
     });
}

private void UpdateOpenedWindow()
{
     if (!OpenedWindow.Dispatcher.CheckAccess())
     {
         //Update UI like this really visual update UI
         OpenedWindow.Dispatcher.Invoke(() => OpenedWindow.IsEnabled = false);
         Thread.Sleep(1000);
         OpenedWindow.Dispatcher.Invoke(() => OpenedWindow.IsEnabled = true);

         //But if I use only one Invoke block, UI not visual update.. Why? In WinForm this work with no problem
         //OpenedWindow.Dispatcher.Invoke(() =>
         //{
         //    OpenedWindow.IsEnabled = false
         //    Thread.Sleep(1000);
         //    OpenedWindow.IsEnabled = true
         //});
     }
     else
     {
         //This not work in WPF. Window seems to stay in enabled state ever
         OpenedWindow.IsEnabled = false;
         Thread.Sleep(1000);
         OpenedWindow.IsEnabled = true;
     }
}

In WinForm all comment statements work well and update UI. In WPF not happens. And I would like to understand why and why setting IsEnabled to false and then to true without invoke, task, ecc..., not really update UI in WPF. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source