1

我正在使用第三方工具,其中我得到一个 InvalidOperationException (实际上,这最终发生在 PresentationFramework.dll 中):

调用线程无法访问此对象,因为不同的线程拥有它。

我尝试了使用 Invoke 的任何变体,包括 BeginInvoke,但没有任何变化。

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

使用 Google 时,我只找到建议使用 Invoke 的“解决方案”。好吧,我确实使用 Invoke。
stackoverflow 上的其他问题及其各自的答案也无济于事。

为了追查真正的原因,我还能做些什么?


编辑:我又查看了线程窗口,完整的调用堆栈在主线程中。AFAIK,这表明调用是多余的。


Edit2:
在调用 open 时不会直接引发错误。ThirdPartyTool 初始化一个列表框,当测量这个列表框时,展示框架中出现错误:

在此处输入图像描述

实际异常被包装到 XamlParseException 中。完整的异常细节:

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
4

2 回答 2

6

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);
                   } ));
于 2013-06-07T09:59:06.200 回答
1

事实证明,我的雷达屏幕上出现的图像导致了这个问题。一旦我们打电话Freeze,问题就消失了。

于 2013-09-12T13:44:32.573 回答