I have a window:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Horizontal" Background="{Binding BackgroundColor}">
<Button Content="Button1" Click="ButtonBase_OnClick"/>
</StackPanel>
</Window>
And this CodeBehind for the window:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WpfApplication1.Annotations;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private Brush _backgroundColor;
public MainWindow()
{
InitializeComponent();
DataContext = this;
Background = Brushes.Orange;
}
public Brush BackgroundColor
{
get { return _backgroundColor; }
set
{
_backgroundColor = value;
OnPropertyChanged();
}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Background = Brushes.Yellow;
Thread.Sleep(5000);
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
So basically I click a button and change background color. Everything works fine, unless I'm blocking the UI Thread.
When I do something like Background = Brushes.Yellow;Thread.Sleep(5000);
I expect the background color to change, and then UI to freeze.
But currently UI seems not to be able to re-render itself before freezing and the color is changed after Sleep()
release the lock.
I've tried to play around with Dispatcher, setting the priority, but behavior seems to be the same.
Anyone got ideas how to set for Sleep()
lower priority, so the UI would be updated completely before goining to sleep?
P.S. The given code is just a quick reproduction of my real case, where I'm from WPF application starting another WPF application process.