none
WPF TreeView Exception

    Question

  • Gurus,

    I've got an exception for which I have no clue of how to debug. Firstly, I have a WPF TreeView that I build using this multithreading technique. Everything's working great, and I can remove and add nodes untill I programatically change the Foreground color of a tree node and the exception below is thrown. The weird part is that the color of the node is set, and all code is execuited and and eventually the exception is throw from the STAThread's static main method. It seems to be that the error is being thrown when the tree is being refreshed.

    Does anyone have any pointers?

    Thanks

    Code Snippet

    System.InvalidOperationException was unhandled
      Message="Cannot use a DependencyObject that belongs to a different thread than its parent Freezable."
      Source="WindowsBase"
      StackTrace:
           at System.Windows.Freezable.EnsureConsistentDispatchers(DependencyObject owner, DependencyObject child)
           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.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.Controls.Control.ArrangeOverride(Size arrangeBounds)
           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.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.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 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at fafs.fcat.Program.Main() in C:\Documents and Settings\ashirazi\My Documents\Expression\Expression Blend Projects\FCAT\Program.cs:line 18
           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()
      InnerException:


    Wednesday, April 09, 2008 2:52 AM

Answers

  • Without seeing your code I'm guessing that your foreground color is a brush created on one thread and is being applied to your treeview in another thread.

    You should be able to call Freeze on your brush which will make it safe to apply in any thread.

     

    -- Ifeanyi Echeruo [MSFT]

    Wednesday, April 09, 2008 11:05 PM

All replies

  • Hi,

     

    Post Moved from 'Visual C#' to 'WPF' forums.

     

     

    HTH,
    Suprotim Agarwal

    -----
    http://www.dotnetcurry.com
    -----

    Wednesday, April 09, 2008 4:09 AM
    Moderator
  • Without seeing your code I'm guessing that your foreground color is a brush created on one thread and is being applied to your treeview in another thread.

    You should be able to call Freeze on your brush which will make it safe to apply in any thread.

     

    -- Ifeanyi Echeruo [MSFT]

    Wednesday, April 09, 2008 11:05 PM
  • On the DOT!!!!
    Thanks,
    Thursday, April 10, 2008 12:51 AM