Have different default PropertyChanged and CollectionChanged despatchers

This is because PropertyChanged events can be async - WPF handles the
marshalling. However if CollectionChanged events are async, then the
thing listening to the event can still be iterating the collection while
the user's editing it, leading to irritating little exceptions
This commit is contained in:
Antony Male 2014-12-05 10:59:08 +00:00
parent 94d4a31b85
commit 4a2c345bec
3 changed files with 20 additions and 4 deletions

View File

@ -41,7 +41,7 @@ namespace Stylet
{
private Action<Action> _propertyChangedDispatcher = Execute.DefaultPropertyChangedDispatcher;
/// <summary>
/// Dispatcher to use when firing events. Defaults to Execute.DefaultPropertyChangedDispatcher
/// Dispatcher to use when firing events. Defaults to BindableCollection.DefaultPropertyChangedDispatcher
/// </summary>
public Action<Action> PropertyChangedDispatcher
{
@ -49,6 +49,17 @@ namespace Stylet
set { this._propertyChangedDispatcher = value; }
}
private Action<Action> _collectionChangedDispatcher = Execute.DefaultCollectionChangedDispatcher;
/// <summary>
/// Dispatcher to use when firing CollectionChanged events. Defaults to BindableCollection.DefaultCollectionChangedDispatcher
/// </summary>
public Action<Action> CollectionChangedDispatcher
{
get { return this._collectionChangedDispatcher; }
set { this._collectionChangedDispatcher = value; }
}
/// <summary>
/// We have to disable notifications when adding individual elements in the AddRange and RemoveRange implementations
/// </summary>

View File

@ -71,9 +71,14 @@ namespace Stylet
private static bool? inDesignMode;
/// <summary>
/// Default dispatcher used by PropertyChangedBase instances. Defaults to OnUIThread
/// Default dispatcher used by PropertyChanged events. Defaults to OnUIThread
/// </summary>
public static Action<Action> DefaultPropertyChangedDispatcher = Execute.OnUIThreadSync;
public static Action<Action> DefaultPropertyChangedDispatcher = a => a();
/// <summary>
/// Default dispatcher used by CollectionChanged events. Defaults to OnUIThreadSync
/// </summary>
public static Action<Action> DefaultCollectionChangedDispatcher = Execute.OnUIThreadSync;
private static void EnsureDispatcher()
{

View File

@ -99,7 +99,7 @@ namespace StyletUnitTests
}
[Test]
public void UsesExecutesDispatcherByDefault()
public void UsesStaticDispatcherByDefault()
{
Action action = null;
var oldDispatcher = Execute.DefaultPropertyChangedDispatcher;