基本用法
这玩意主要是解决wpf mvvm模型中,当observablecollection绑定到itemscontrol之后就无法在非ui线程中直接修改的问题。
先从测试代码弄起
MainWindow.xaml
<Window x:Class="ForLiveChart.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"
mc:Ignorable="d"
x:Name="root"
Title="MainWindow" Height="450" Width="800">
<ListView ItemsSource="{Binding ElementName=root,Path=testCollection}"></ListView>
</Window>
MainWindow.cs
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace ForLiveChart
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//BindingOperations.EnableCollectionSynchronization(testCollection, locker);//注释则出错
var act = new Action(() =>
{
Thread.Sleep(100);
for (int i = 0; i < 100; i++)
lock(locker)
testCollection.Add(i);
});
Task.Run(act);
Task.Run(act);
Task.Run(act);
Task.Run(act);
Task.Run(act);
}
private object locker = new object();
public ObservableCollection<int> testCollection { get; } = new ObservableCollection<int>();
}
}
我们都知道,当非ui线程修改界面控件绑定好的ObservableCollection的时候会报错:
System.NotSupportedException:“This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.”
当取消掉BindingOperations.EnableCollectionSynchronization的注释的时候,这玩意就可以正常运行了。然而lock(locker)这句并不是必须的,此处因为多线程添加加入,而如果人为控制不会多线程同时修改的话可以不需要。原因不明=.=
上面是简单用法,下面就是是我踩的坑了
参照微软docs的BindingOperations.EnableCollectionSynchronization 方法说明,其中的两个坑:
坑1
调用必须在 UI 线程上发生。
将
BindingOperations.EnableCollectionSynchronization(testCollection, locker);
修改为
Task.Run(() =>
{
BindingOperations.EnableCollectionSynchronization(testCollection, locker);
});
在ui线程中修改testCollection没问题,但是在非ui线程对TestCollection的修改会报错:
System.NotSupportedException:“This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.”
和之前的报错一毛一样~~~
坑2(其实发生的问题和这句话没关系,但是挺像的)
在另一线程上使用集合之前,或在将集合附加到之前,必须先调用 ItemsControl ,以之后的为准
代码改一下,将构建控件的InitializeComponent移到构造函数最后
public MainWindow()
{
BindingOperations.EnableCollectionSynchronization(testCollection, locker);
var act = new Action(() =>
{
lock(locker)
testCollection.Add(1);
});
Task.Run(act).Wait();
InitializeComponent();
}
然后执行,发现并没有什么发生,还是正常执行了
但是如果再~~改一下:
public MainWindow()
{
BindingOperations.EnableCollectionSynchronization(testCollection, locker);
var act = new Action(() =>
{
lock (locker)
Dispatcher.Invoke(() =>
{
testCollection.Add(1);
});
});
Task.Run(act);
InitializeComponent();
}
铛铛铛铛~ 死锁上线~
而如果将InitializeComponent()上移
public MainWindow()
{
InitializeComponent();
BindingOperations.EnableCollectionSynchronization(testCollection, locker);
var act = new Action(() =>
{
lock (locker)
Dispatcher.Invoke(() =>
{
testCollection.Add(1);
});
});
Task.Run(act);
}
或者主界面不绑定testCollection:
<Window x:Class="ForLiveChart.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:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
x:Name="root"
Title="MainWindow" Height="450" Width="800">
<!--<ListView ItemsSource="{Binding ElementName=root,Path=testCollection}"></ListView>-->
</Window>
都可以正常执行
大概分析下:ItemsControl的ItemsSource设定会lock(locker),而工作线程中的lock(locker)中有Dispatcher.Invoke,所以工作线程在lock(locker)之后的Dispatcher.Invoke会等待界面线程的完成,而界面线程中设定ItemsSource又会等待lock的解锁,导致互锁。
踏坑分析
这个坑是上俩坑结合才会发生的。
我有一个类需要异步创建,并且这个类里包含observablecollection,并且需要binding到界面
xaml:
<Window x:Class="ForLiveChart.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"
mc:Ignorable="d"
x:Name="root"
Title="MainWindow" Height="450" Width="800">
<ListView ItemsSource="{Binding ElementName=root,Path=Device.Children}"></ListView>
</Window>
cs:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace ForLiveChart
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
var act = new Action(() =>
{
Device.AddChild();
});
Task.Run(act);
InitializeComponent();
}
public Device Device { get; set; } = new Device();
}
public class Device
{
public Device()
{
Application.Current.Dispatcher.Invoke(() =>
{
BindingOperations.EnableCollectionSynchronization(Children, locker);
});
}
public ObservableCollection<Device> Children { get; } = new ObservableCollection<Device>();
object locker = new object();
public void AddChild()
{
lock (locker)//演示,多线程对collection的操作最好加锁或者选用带锁容器
{
Children.Add(new Device());
}
}
}
}
解决方法也很简单。。
- 直接先InitializeComponent()创建控件即可
- 将AddChild函数改为
var dev=new Device();
lock (locker)//演示,多线程对collection的操作最好加锁或者选用带锁容器
{
Children.Add(dev);
}
- 在危险的地方放弃使用BindingOperations.EnableCollectionSynchronization全部手动着来。
总结
- 有可能对ui或者ui绑定对象进行的操作,最好要放到InitializeComponent之后。
- BindingOperations.EnableCollectionSynchronization是个好同志,但是如果涉及到工作线程创建的话就要小心再小心了,指不定哪个循环调用引用什么的就把你绕进去了。
- lock中包含尽量少的内容特别是创建某些有复杂的构造函数了类,有时候多写两行代码又清晰,思路又明确,还能绕开很多坑
- 以上都是我胡言乱语的,千万别信。