GraphLayout.Graph thread access issue on DoNotificationLayout

Jan 6, 2011 at 6:28 PM

Hi All

I've been trying to use GraphSharp to display the hierarchical relation between different datasets. This all works fine until I start adding datasets at runtime. At some point in the adding process I get the following exception

System.InvalidOperationException was unhandled
  Message=The calling thread cannot access this object because a different thread owns it.
  Source=WindowsBase
  StackTrace:
       at System.Windows.Threading.DispatcherObject.VerifyAccess()
       at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
       at GraphSharp.Controls.GraphLayout`3.get_Graph()
       at GraphSharp.Controls.GraphLayout`3.CreateVertexControl(TVertex vertex)
       at GraphSharp.Controls.GraphLayout`3.OnMutation()
       at GraphSharp.Controls.GraphLayout`3.b__24(Object s, RunWorkerCompletedEventArgs e)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
  InnerException: 

From the looks of it this happens because GraphLayout.Graph is a dependency property on a UI control which is being accessed by a non-UI thread. The interesting thing is that I call the NotifyPropertyChanged method on the UI thread but then graphsharp pushes the layout updating onto a background thread (which is obviously not the UI thread).

Does anybody know how to solve this problem? I suspect that either graphsharp has to get the graph through a non-dependency property or some of the work has to be moved to the UI thread.

Thanks

Patrick

Jan 15, 2011 at 3:10 AM

It turns out it was a problem with the SynchronizationContext on the UI thread. Something my app was doing was removing the WPF version (i.e. DispatcherSynchronizationContext) which means that the first time it was needed again a default context was made. Obviously that was the wrong thing to do because that caused the GraphLayout.OnMutation to be run on the incorrect thread.

Anyway I hope this helps somebody else :)

Jun 29, 2012 at 4:24 PM

I realize this is a very old thread, but I was wondering if you could say what you did to fix the issue. I am having the same problem and I understand generally what the issue is. But I don't know how to set the WPF version of the SynchronizationContext and/or prevent my app from removing it. Just wondering if you could tell me what you had to do to fix it.

Thanks.