Few issues with threading

Feb 9, 2015 at 10:26 PM

I love graphvzs4net - it is exactly what I've been looking for. I am having a few issues however.

I have basically followed the example implementation. One thing I noticed immediately is the example never needs to do Dispatcher.Invoke(...) to make changes to the graph, but I do for some reason. Is there any explanation for this? Is this something to be worried about? I.e. in order to get my program to run without throwing the exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll

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

I need to do the following to everything that operates on the graph:
Application.Current.Dispatcher.BeginInvoke(new Action(() => MeshGraph.AddVertex(myVertex) ));
My main issue is that I have events coming in which need to update the graph. I cannot control how fast these events happen (but they are in fairly quick succession). Most of the time the updates work properly and the graph draws properly, but once and a while the graph drawing dissapears and I get:

"Graphviz4Net: an exception was thrown during layouting.Exception message: LayoutDirector: the RunDot method must be invoked before call to BuildGraph."

Is there an easy way to prevent this?
Feb 9, 2015 at 10:34 PM
Hi teldridge,

nice to hear that you like GraphViz4Net! Now to your question:

Where do these events come from? I am guessing it is from TCP/UDP connection or something else than UI events, right? If so, it is nothing to be worried about, you would have to do that with any code that does something with any UI controls. It has to run on the UI thread only.

If you did not want to use Dispatcher.BeginInvoke, you can play around with SynchronizationContext, but that is out of the scope of Graphviz4net and this discussion :-)
Feb 9, 2015 at 10:43 PM
Edited Feb 9, 2015 at 10:44 PM
Wow, that was a quick response! Thank you very much. You are 100% correct - the events are not being generated from the UI and your explanation totally makes sense.

I do notice when the updates come in very quickly (and therefore I'm adding vertexes etc. quickly) occasionally I'll get the RunDot method must be invoked before call to BuildGraph exception shown on the canvas in place of the graph. It seems to happen when perhaps the graph has not finished drawing and there is an update. All I can think to do is delay the updates until the graph is finished drawing the previous update - is there some way I check if it is safe to go ahead with an update to the drawing? Or would it be better to simply update the graph on a slow timer?
Feb 9, 2015 at 10:51 PM
You are right. UpdateVerticesLayout class in method UpdateLayout is not checking whether there is already a thread running dot.exe on the background. Only workaround I can think of now is to delay your events a little.
Feb 9, 2015 at 10:53 PM
That is helpful to know. Thanks again for your very quick responses!