Dispatcher update of a CollectionViewSource.Source throws wrong thread exception while on same thread
By : Agam Saputra
Date : March 29 2020, 07:55 AM
this will help I had the same thread issues while binding directly to a CollectionViewSource. I fixed the problem by binding my listbox to a CollectionView object instead. In your case, you would simply need to change your listbox declaration to: code :
<ListBox x:Name="MessageList" ItemsSource="{Binding MessagesView.View}">
|
After async/await change, using Dispatcher.Invoke() doesn't solve CollectionView changes other than Dispatcher thread
By : Ricky Tan
Date : March 29 2020, 07:55 AM
like below fixes the issue Sounds like your collection view is being created on a background thread, which is not a good idea in general. If you're loading data using async/await (and your data loading supports asynchronous operations), then you shouldn't need to use background threads.
|
Application.Current.Dispatcher vs DependencyObject.Dispatcher for UI Thread Access
By : Ankit
Date : March 29 2020, 07:55 AM
To fix the issue you can do Not all DispatcherObject instances are necessarily created on the primary UI thread (some applications use a thread per Window approach). I would personally advise using DispatcherObject.Dispatcher.BeginInvoke as regardless of which thread created the entity, the use of the DispatcherObject in the delegate will succeed. code :
// Will always work
myTextBox.Dispatcher.BeginInvoke(new Action(myTextBox.Focus));
// May fail if myTextBox was created on a different thread
Application.Current.Dispatcher.BeginInvoke(new Action(myTextBox.Focus));
|
Calling Dispatcher.Invoke() on Dispatcher thread
By : user2447356
Date : March 29 2020, 07:55 AM
help you fix your problem I suspect the issue here is a wrong .NET synchronization context (or lack of thereof) in the 1st case. As far as I understand, this code is called from an unmanaged host, which naturally doesn't have a .NET synchronization context installed on the main UI thread. code :
System.Diagnostics.Debug.WriteLine(new { System.Threading.SynchronizationContext.Current });
window.Show(); // throws InvalidOperationException
Dispatcher.CurrentDispatcher.Invoke(() => {
System.Diagnostics.Debug.WriteLine(new { System.Threading.SynchronizationContext.Current });
window.Show();
});
|
Use of Application.Current.Dispatcher for events outside dispatcher thread
By : Nathan VS
Date : March 29 2020, 07:55 AM
This might help you Should "Application.Current.Dispatcher.Invoke" be put in the worker thread raising an event or in the UI code handling the event?
|