DrawGraph on Vertexdoubleclick event

Apr 16, 2013 at 6:53 AM
Edited Apr 16, 2013 at 6:55 AM

My objective is to draw a graph and on double clicking on any vertex should draw another graph. The code for vertex double click event is

private void eventTraceNodeVisualizer_VertexDoubleClick(object sender, VertexEventArgs vertexEventArgs)
private void DrawGraph(string nodeId)
        Logic.LoadEventTrace eventTraceObj = new Logic.LoadEventTrace();
        Logic.Element elementObj = new Logic.Element();
        elementObj = eventTraceObj.ReadNodeMachines(nodeId);
        NodeXLControl nodeXlControl = new NodeXLControl();

            IVertex headVertex = eventTraceNodeVisualizer.Graph.Vertices.Add();
            headVertex.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Circle);
            headVertex.SetValue(ReservedMetadataKeys.PerVertexLabel, "Event Trace");
            headVertex.SetValue(ReservedMetadataKeys.PerVertexLabelFillColor, System.Drawing.Color.Blue);
            foreach (string element in elementObj.childVertex)
                IVertex childVertex = eventTraceNodeVisualizer.Graph.Vertices.Add();
                childVertex.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Triangle);
                childVertex.SetValue(ReservedMetadataKeys.PerVertexLabel, elementObj.childVertex);
                childVertex.SetValue(ReservedMetadataKeys.PerVertexLabelFillColor, System.Drawing.Color.Red);
                IEdge childEdge = eventTraceNodeVisualizer.Graph.Edges.Add(headVertex, childVertex);
                childEdge.SetValue(ReservedMetadataKeys.PerColor, System.Drawing.Color.Green);

            eventTraceNodeVisualizer.Layout = new SugiyamaLayout();

Now the issue is whenever I am double clicking on the vertex it throws this error

Smrf.NodeXL.Visualization.Wpf.NodeXLControl.SetVertexSelected: An asynchronous layout is in progress. Check the IsLayingOutGraph property before calling this.

Any idea how can I solve this? I got to see one more thread regarding this where the user removed the loop logic and was able to redraw a graph. I want the loop in my logic. Any help will really be appreciated

Apr 16, 2013 at 8:47 AM
Edited Apr 16, 2013 at 8:52 AM

NodeXL explicitly protects itself against what you are trying to do. In case you're wondering why, I'll quote from the source code for the NodeXLControl.SetVertexSelected() method:
   // The reason that vertices and edges can't be selected while the graph
   // is being laid out is that selecting them involves modifying
   // their metadata.  The graph layout code, which runs on a worker
   // thread, is allowed to modify metadata during the layout, so having
   // the worker thread and this foreground thread modifying metadata
   // simultaneously would lead to synchronization clashes.
But how about this as an alternative to what you're doing now: In your VertexDoubleClick event handler, check the NodeXLControl.IsLayingOutGraph property. If it's true, set a timer and return without doing anything else. When the timer goes off, check the property again. If it's false, modify and redraw the graph. Otherwise, set the timer again.

I haven't tried this, but it might work.

-- Tony
Apr 16, 2013 at 5:50 PM
Thanks Tony, let me give a try
Apr 23, 2013 at 12:35 AM
Hi Tony,

I think I have resolved the issue.

I created an usercontrol which has two constructors. One was the default one which was used to construct the graph initially and the other one was the parametrized one, which took the node Tag which was clicked. In my main form I used a stackpanel where I am calling the usercontrol based on their types.

This is cool, learnt many things meanwhile :)