WPF + PostSharp’ed View Model get frozen in a minute

dnogin's Avatar

dnogin

17 Sep, 2018 02:43 AM

WPF starts ignoring property change notifications after a minute of intensive work. Here is a demo to reproduce (also on GitHub). View model is:

[NotifyPropertyChanged]
[ContentProperty("Tests")]
public class Model
{
    public List<Test> Tests { get; } = new List<Test>();
    public Test Test { get; set; }
}

Where:

[NotifyPropertyChanged]
public class Test
{
    public string Name { get; set; }
}

XAML:

<Window x:Class="Demo.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:Demo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:Model>
            <local:Test Name="a"/>
            <local:Test Name="b"/>
            <local:Test Name="c"/>
            <local:Test Name="d"/>
        </local:Model>
    </Window.DataContext>
    <TextBox DockPanel.Dock="Top" Text="{Binding Test.Name, Mode=OneWay}"/>
</Window>

And the code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Timer = new DispatcherTimer();
        Timer.Interval = TimeSpan.FromMilliseconds(250);
        Timer.Tick += Timer_Tick;
        Timer.Start();
    }

    DispatcherTimer Timer { get; }
    Random Random = new Random();
    Model Model => DataContext as Model;

    private void Timer_Tick(object sender, EventArgs e)
    {
        var i = Random.Next(Model.Tests.Count);
        Model.Test = Model.Tests[i];
    }
}

Run it and wait a minute – window will get frozen.

Here is how I bypassed the problem for now - it looks working:

[NotifyPropertyChanged]
public class Test : INotifyPropertyChanged
{
    public string Name { get; set; }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    void OnPropertyChanged(string propertyName) => 
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
  1. Support Staff 1 Posted by PostSharp Techn... on 20 Sep, 2018 04:08 PM

    PostSharp Technologies's Avatar
  2. PostSharp Technologies closed this discussion on 20 Sep, 2018 04:08 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac