I have a extension for an application, so that my program is actually a DLL that was created in WPF. This extension works with Windows Explorer. So, when i open first time, everything seems fine but i open a new windows explorer and then a different thread is doing process as normal. But it causes problem. I used Dispatcher invoke but i still got the same problem
// Lazy load the child items, if necessary.
if (!this.HasLoadedChildren)
{
try
{
Trace.WriteLine("Thread ID: "+ Dispatcher.CurrentDispatcher.Thread.ManagedThreadId);
this.Children.Remove(DummyChild);
}
catch (Exception e)
{
System.Windows.Threading.Dispatcher.CurrentDispatcher
.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
(Action) delegate ()
{
this.Children.Remove(DummyChild);
});
}
Well I trace the current thread ID, in the begining its "1" and when i open the other explorer window for my app then i see that the thread id is "3" and then i got this exception.
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.ObjectModel.ObservableCollection
1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection
1.RemoveItem(Int32 index) at System.Collections.ObjectModel.Collection`1.Remove(T item) at RMView.ViewModel.TreeViewItemViewModel.set_IsExpanded(Boolean value) in C:\Users\xyz\Development...\TreeViewItemViewModel.cs:line 92
I tried to use Application.Current.Dispatcher but its null, i think because my app is a dll, not a windows exe application.
Where am i doing wrong?