0

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.ObservableCollection1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection1.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?

ertan2002
  • 1,458
  • 1
  • 32
  • 68
  • What about this.Dispatcher? Is it also null? – mm8 Mar 28 '17 at 09:44
  • @mm8, sorry for the late answer. Well I tried it also in form, its not null but it also does not work. My code above, it is in a class so there is no "this" keyword but i also tried in the form, it does not work – ertan2002 Mar 28 '17 at 15:39

0 Answers0