Question

I am using a third party tool in which I get an InvalidOperationException (actually, in the end this occurs in PresentationFramework.dll):

The calling thread cannot access this object because a different thread owns it.

I tried any kind of variations using Invoke, including BeginInvoke, but nothing changes.

Session session = new ThirdPartyTool.Session();
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => session.Open(view)));

When using Google I only find "solutions" that suggest to use Invoke. Well, I do use Invoke.
Other questions and their respective answer here on stackoverflow didn't help either.

What can I still do in order to track down the actual cause?


Edit: I had another look into the thread window and the full call stack is in the main thread. AFAIK, this indicates the Invoke is superfluous.


Edit2:
The error isn't raised directly on calling open. The ThirdPartyTool initializes a list box and when measuring this list box the error occurs in the presentation framework:

enter image description here

The actual exception is wrapped into an XamlParseException. The full exception detail:

System.Windows.Markup.XamlParseException occurred  
HResult=-2146233087  
Message=The calling thread cannot access this object because a different thread owns it.  
Source=PresentationFramework  
LineNumber=0  
LinePosition=0  
StackTrace:  
  at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)  
InnerException: System.InvalidOperationException  
  HResult=-2146233079  
  Message=The calling thread cannot access this object because a different thread owns it.  
  Source=WindowsBase  
  StackTrace:  
    at System.Windows.Threading.Dispatcher.VerifyAccess()  
    at System.Windows.Freezable.get_IsFrozen()  
    at System.Windows.Controls.Image.UpdateBaseUri(DependencyObject d, ImageSource source)  
    at System.Windows.Controls.Image.OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
    at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)  
    at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)  
    at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)  
    at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)  
    at System.Windows.FrameworkTemplate.ReceivePropertySet(Object targetObject, XamlMember member, Object value, DependencyObject templatedParent)  
    at System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__4(Object sender, XamlSetValueEventArgs setArgs)  
    at System.Xaml.XamlObjectWriter.OnSetValue(Object eventSender, XamlMember member, Object value)  
    at System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent)  
    at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)  
    at System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx)  
    at System.Xaml.XamlObjectWriter.WriteEndObject()  
    at System.Xaml.XamlWriter.WriteNode(XamlReader reader)  
    at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)  
  InnerException: null
Was it helpful?

Solution 2

It turned out that an image which was off my radar screen was causing the issue. Once we called Freeze, the issue disappeared.

OTHER TIPS

I'd hazard a guess and suggest that you don't use invoke: just call session.Open from where you are.

I say that because - if your session object has thread affinity - you've just created it on whatever the current thread is, and so the Open call needs to be on the same thread. Your Invoke is potentially pushing the call elsewhere.

Alternatively, it could be that some other code is causing the problem. If that's the case then you could instead try this to create the object on whatever the dispatcher thread is:

Session session = null;
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
    (Action)(() => { 
                     session = new ThirdPartyTool.Session();
                     session.Open(view);
                   } ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top