Skip to content

Commit

Permalink
New .Net 4.5 Feature
Browse files Browse the repository at this point in the history
No longer using the Dispatcher thread to access ObservableCollection from Non-UI Background Thread:

https://docs.microsoft.com/en-us/dotnet/framework/wpf/getting-started/whats-new#xthread_access
  • Loading branch information
Dirkster99 committed Jun 12, 2018
1 parent aa98bde commit 26f1b5f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;

/// <summary>
/// Implements the root viewmodel of the tree structure to be presented in a treeview.
/// </summary>
public class MetaLocationRootViewModel : Base.BaseViewModel
{
#region fields
private const DispatcherPriority _ChildrenEditPrio = DispatcherPriority.DataBind;

protected readonly ObservableCollection<MetaLocationViewModel> _CountryRootItems = null;
protected readonly IList<MetaLocationViewModel> _BackUpCountryRoots = null;

private ICommand _ExpandCommand;

private object _itemsLock = new object();
#endregion fields

#region constructors
Expand All @@ -33,6 +32,8 @@ public class MetaLocationRootViewModel : Base.BaseViewModel
public MetaLocationRootViewModel()
{
_CountryRootItems = new ObservableCollection<MetaLocationViewModel>();
BindingOperations.EnableCollectionSynchronization(_CountryRootItems, _itemsLock);

_BackUpCountryRoots = new List<MetaLocationViewModel>(400);
}
#endregion constructors
Expand Down Expand Up @@ -109,14 +110,13 @@ string zipContainerFile

foreach (var item in isoCountries)
{
Application.Current.Dispatcher.Invoke(() =>
lock (_itemsLock)
{
var vmItem = MetaLocationViewModel.GetViewModelFromModel(item);

RootsAdd(vmItem, true);
//Root.RootsAdd(vmItem, false); // Make all items initially visible

}, DispatcherPriority.ApplicationIdle);
}
}
}

Expand Down Expand Up @@ -155,7 +155,10 @@ public int DoSearch(SearchParams searchParams, CancellationToken token)
searchParams.SearchStringTrim();
searchParams.SearchStringToUpperCase();

Application.Current.Dispatcher.Invoke(() => { root.Clear(); }, _ChildrenEditPrio);
lock (_itemsLock)
{
root.Clear();
}

// Show all root items if string to search is empty
if (searchParams.IsSearchStringEmpty == true ||
Expand All @@ -170,7 +173,10 @@ public int DoSearch(SearchParams searchParams, CancellationToken token)
rootItem.ChildrenClear(false);
rootItem.SetExpand(false);

Application.Current.Dispatcher.Invoke(() => { root.Add(rootItem); }, _ChildrenEditPrio);
lock (_itemsLock)
{
root.Add(rootItem);
}
}

return 0;
Expand Down Expand Up @@ -217,7 +223,11 @@ public int DoSearch(SearchParams searchParams, CancellationToken token)
rootItem.SetExpand(false);

//Console.WriteLine("node: {0} match count: {1}", rootItem.LocalName, nodeMatchCount);
Application.Current.Dispatcher.Invoke(() => { root.Add(rootItem); }, _ChildrenEditPrio);
lock (_itemsLock)
{
root.Add(rootItem);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Data;

/// <summary>
/// Implements an items viewmodel that should be bound to an items collection in a tree view.
/// </summary>
public class MetaLocationViewModel : Base.BaseViewModel, IHasDummyChild
{
#region fields
private static DispatcherPriority _ChildrenEditPrio = DispatcherPriority.Normal;

private static readonly MetaLocationViewModel DummyChild = new MetaLocationViewModel();

private bool _IsItemExpanded;
Expand All @@ -28,6 +25,8 @@ public class MetaLocationViewModel : Base.BaseViewModel, IHasDummyChild
private MatchType _Match;
private string _LocalName;
private ISelectionRange _Range = null;

private object _itemsLock = new object();
#endregion fields

#region constructors
Expand Down Expand Up @@ -56,10 +55,11 @@ BusinessLib.Models.MetaLocationModel locationModel
/// </summary>
protected MetaLocationViewModel()
{
//// _IsItemVisible = true;
_IsItemExpanded = false;

_Children = new ObservableCollection<MetaLocationViewModel>();
BindingOperations.EnableCollectionSynchronization(_Children, _itemsLock);

_BackUpNodes = new List<MetaLocationViewModel>();

_Match = MatchType.NoMatch;
Expand Down Expand Up @@ -377,15 +377,14 @@ internal void ChildrenClear(bool bClearBackup = true
{
try
{
Application.Current.Dispatcher.Invoke(() =>
lock (_itemsLock)
{
_Children.Clear();

// Cities do not have children so we need no dummy child here
if (bAddDummyChild == true && TypeOfLocation != LocationType.City)
_Children.Add(DummyChild);

}, _ChildrenEditPrio);
}

if (bClearBackup == true)
_BackUpNodes.Clear();
Expand Down Expand Up @@ -487,10 +486,16 @@ private void ChildrenAdd(MetaLocationViewModel child, bool bAddBackup = true)
{
if (HasDummyChild == true)
{
Application.Current.Dispatcher.Invoke(() => { _Children.Clear(); }, _ChildrenEditPrio);
lock (_itemsLock)
{
_Children.Clear();
}
}

Application.Current.Dispatcher.Invoke(() => { _Children.Add(child); }, _ChildrenEditPrio);
lock (_itemsLock)
{
_Children.Add(child);
}

if (bAddBackup == true)
_BackUpNodes.Add(child);
Expand All @@ -511,11 +516,14 @@ private void ChildrenAddBackupNodes(MetaLocationViewModel child)

private void ChildrenRemove(MetaLocationViewModel child, bool bRemoveBackup = true)
{
Application.Current.Dispatcher.Invoke(() => { _Children.Remove(child); }, _ChildrenEditPrio);
lock (_itemsLock)
{
_Children.Remove(child);
}

if (bRemoveBackup == true)
_BackUpNodes.Remove(child);
}
#endregion methods
#endregion methods
}
}

0 comments on commit 26f1b5f

Please sign in to comment.