Version

ThreadSynchronizationCollection Property

Returns the collection to use to synchronize thread access when using the AllValues enumerable.
Syntax
'Declaration
 
Public Overridable ReadOnly Property ThreadSynchronizationCollection As IEnumerable
public virtual IEnumerable ThreadSynchronizationCollection {get;}
Remarks

In version 4.5 of the .NET framework Microsoft added support for cross thread updating and access of a collection via the BindingOperations' EnableCollectionSynchronization and AccessCollection methods. In this scenario, the ThreadSynchronizationCollection property can be used to safely iterate over the AllValues property. e.g.:

             
              BindingOperations.AccessCollection(context.ThreadSynchronizationCollection, new Action(() =>
                  {
                      foreach (ValueEntry ii in context.AllValues)
                      {
                          /// ... process the entry
                      }
                  }), false);
                  
             

Note: BindingOperations.EnableCollectionSynchronization which was added in 4.5 must be called on every thread that needs to call its AccessCollection method and it must to be called with the same locking object on each thread for a given collection, e.g.:

             
               var list = new ObservableCollection<MyClass>();
            
               var lockingObj = new object();
               
               // Call EnableCollectionSynchronization on the UI thread
               BindingOperations.EnableCollectionSynchronization(list, lockingObj);
               
               // Note: that XamDataGrid will only support cross-thread updating if the list is in a CollectionView
               XamDataGrid1.DataSource = CollectionViewSource.GetDefaultView(list);
               
               // perform updating of the collection on a background thread using locks
               Task.Run(new Action(() =>
               {
                   // lock using the same locking object that was passed into EnableCollectionSynchronization
                   // above on the UI thread
                   lock(lockingObj)
                   {
                       list.Add(new MyClass());
                   }
               }));
               
               // Alternatively, you can perform updating of the collection on a background thread using AccessCollection, e.g.:
               Task.Run(new Action(() =>
               {
                   // Since we will be calling AccessCollection on this thread we need to first call
                   // EnableCollectionSynchronization with the same locking object
                   BindingOperations.EnableCollectionSynchronization(list, lockingObj);
            
                   // calling AccessCollection will wrap the action in a synchronization lock
                   BindingOperations.AccessCollection(list, new Action(() =>
                   {
                       list.Add(new MyClass());
                   }), true);
               }));
             

Requirements

Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also