Managing Multi-Thread Data Updates
- 4 minutes to read
This topic describes how to manage multi-thread data updates.
Dispatch Updates to the Main Thread
If the data updating process takes place in another thread, and your business logic does not depend on when separate data updates are executed, you can transfer these updates to the main thread using the Dispatcher.BeginInvoke method:
//another thread
Dispatcher.BeginInvoke(new Action(() => Customers.Add(new Customer { Name = "Bill" }));
Bulk Updates
Handle data updates at the view model level and pass data to the GridControl in the main thread.
Note
The GridControl refreshes its UI and internal state each time its data source collection is updated. Depending on the size of your data source, the amount and frequency of updates, it may be useful to lock the GridControl and update it after all the data is updated. You can use the DataControlBase.BeginDataUpdate and DataControlBase.EndDataUpdate methods to prevent the control’s internal data updates. Refer to Performing Multiple Data Updates for more information.
ICollectionView
You can wrap your collection into the CollectionView and enable collection synchronization using the BindingOperations.EnableCollectionSynchronization method:
public class ViewModel {
private object _syncLock = new Object();
public ObservableCollection<Customer> Customers { get; set; }
public ICollectionView Collection { get; set; }
public ViewModel() {
Customers = new ObservableCollection<Customer>();
for (int i = 0; i < 50; i++) {
Customers.Add(new Customer() { ID = i, Name = "Name" + i, IsChecked = i % 2 != 0 });
}
Collection = CollectionViewSource.GetDefaultView(Customers);
BindingOperations.EnableCollectionSynchronization(Customers, _syncLock);
}
public void Button_Click(object sender, RoutedEventArgs e) {
var thread = new Thread(() => {
lock (_syncLock) {
Customers.Add(new Customer());
}
});
thread.Start();
}
}
Refer to Binding to ICollectionView for more information on how to bind to ICollectionView.
DisableThreadingProblemsDetection Property
You can set the DXGridDataController.DisableThreadingProblemsDetection property to true to disable cross-thread exceptions and force data updates to be sent to the main thread.
Important
Setting DXGridDataController.DisableThreadingProblemsDetection to true will only remove the exception and will not prevent issues related to upgrading the source collection from a separate thread when the GridControl is updating its state. Use this property at your own risk though, as there is no guarantee that the GridControl will always process changes correctly. For example, if you modify a property during the sort operation, the sorting result may no longer be correct.
Starting from v18.2, the TreeListControl detects cross-thread operations and throws an exception to indicate potential issues with updating data from several threads. You can set the TreeListDataController.DisableThreadingProblemsDetection property to true to disable this exception.
Important
Setting TreeListDataController.DisableThreadingProblemsDetection to true does not prevent the issues.
BeginDataUpdate/EndDataUpdate Methods
When you update a bound collection in another thread, both the GridControl and your code from another thread work with the collection. If you cannot move updates to the main thread, lock data operation in the GridControl to avoid exceptions. To do this, wrap data updates inside DataControlBase.BeginDataUpdate/DataControlBase.EndDataUpdate calls.
Example: How to: Display Data Which is Being Updated on Another Thread
Important
When an end user makes the GridControl update its layout (for example, scrolls the data area) after calling DataControlBase.BeginDataUpdate, the GridControl reflects rows modified in another thread. These rows will not be correctly sorted and filtered because all data operations are locked.
Important
This approach does not work for the TreeListView. Dispatch data updates to the main thread to avoid the issue in this scenario.