【问题标题】:How to debug this error when none of my code shows up in the stack?当我的代码都没有出现在堆栈中时,如何调试此错误?
【发布时间】:2011-04-07 21:31:29
【问题描述】:

我的应用程序有时会出现以下错误:

不能使用与父 Freezable 属于不同线程的 DependencyObject

我知道如何解决这种错误,但在那种情况下,我不知道它发生在哪里,所以我不知道要修复什么...仅异常的堆栈跟踪包含 .NET 框架代码,而不是我的代码(Main 方法除外):

   at System.Windows.Freezable.EnsureConsistentDispatchers(DependencyObject owner, DependencyObject child)
   at System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue, DependencyObject newValue, DependencyProperty property)
   at System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue, DependencyObject newValue)
   at System.Windows.Media.RenderData.PropagateChangedHandler(EventHandler handler, Boolean adding)
   at System.Windows.UIElement.RenderClose(IDrawingContent newContent)
   at System.Windows.Media.VisualDrawingContext.CloseCore(RenderData renderData)
   at System.Windows.Media.RenderDataDrawingContext.DisposeCore()
   at System.Windows.Media.DrawingContext.System.IDisposable.Dispose()
   at System.Windows.Media.RenderDataDrawingContext.Close()
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
   at System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.WrapPanel.arrangeLine(Double v, Double lineV, Int32 start, Int32 end, Boolean useItemU, Double itemU)
   at System.Windows.Controls.WrapPanel.ArrangeOverride(Size finalSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
   at System.Windows.Controls.ItemsPresenter.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at MyApplication.App.Main() in E:\MyApplication\MyApplication\obj\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

我怎样才能找到这个错误的原因?由于应用程序非常复杂,手动检查所有可以在另一个线程中执行的代码并不是一个真正的选择......

任何建议将不胜感激!

【问题讨论】:

  • 这是我见过的最令人印象深刻的堆栈跟踪。

标签: .net wpf multithreading freezable


【解决方案1】:

您是否在后台工作人员的 DOwork 处理程序上动态添加一些 UI 控件?

【讨论】:

  • 不,我不是。该应用程序遵循 MVVM 模式,因此我不直接操作 UI
  • 我不知道,这就是我的问题的重点......我正在寻找一种方法来获取有关该错误的更多信息,因为堆栈跟踪在这种情况下没有帮助
  • 看看下面的这篇文章是否可以帮助你,因为关于这些问题的信息很少。 social.msdn.microsoft.com/forums/en-US/wpf/thread/…
  • 其实我已经找到了解决办法,但确实和你说的帖子关系密切。谢谢;)
  • 据说如果ENDS很好,那么一切都很好..:),快乐编码
【解决方案2】:

您是否尝试过 Reflector Pro(有 14 天试用期)?您可以要求它为 WindowsBase.dll(Freezable 所在的位置)生成调试信息。然后你可以在 EnsureConsistentDispatchers 的 if 语句的 then 部分放置一个断点。 这应该告诉您涉及哪些 DependencyObject。

您是在 GUI 线程之外的另一个线程中创建 DependencyObjects 吗?也许在回调方法中?

【讨论】:

  • 我前段时间已经试过了,所以我的试用期结束了。无论如何,我应该能够使用 .NET Framework 源代码步进,但到目前为止我一直无法使其工作......
  • 我不认为我在工作线程中创建 DO,但是代码库太大而无法检查所有内容,所以我不确定...
【解决方案3】:

好的,我终于找到了解决办法……

在我的一个 ViewModel 中,我正在从主题资源中加载一个画笔列表。在某些情况下,此 ViewModel 是从工作线程中使用的。我假设 XAML 资源字典中定义的 Freezables 被隐式冻结,但显然情况并非如此......所以我只需要显式冻结它们,这就解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 2021-12-24
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    相关资源
    最近更新 更多