Multithreading with rhinocommon

rhinocommon

#1

In multithreading, is it safe to use RhinoApp.MainApplicationWindow.Invoke(new Action(delegate { MyMethodWorkingWithRhinoDoc(parameters); })); to perform actions on rhino doc?


(Giulio Piacentino) #2

It should be a good way to do it.

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com


#3

By my test looks like it works too… But… another question… i’m having some bug report from costumers about rhino crashing while tring to open a 3dm file on idle event of rhino and doing some work on rhino geometries right after RhinoDoc.ReadFile() function.
The call stack that i get is always going to say:
Exception has been thrown by the target of an invocation. —> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Making me wonder that some c++ ptr has been changed while i’m performing a loop in rhino objects right after loading the file… the code on idle event is like this:

public void RhinoAppIdle(object sender, EventArgs e)
        {
            RhinoApp.Idle -= RhinoAppIdle;

            RhinoDoc.ReadFile(fullPathFileToLoad, fileReadOptions);
            //SettingTolleranceOnRhinoDoc
            //SettingDefaultLayerOnRhinoDoc
            //ReadingOffsetsFromAXml
            Transform translation = Transform.Translation(offsetX, offsetY, offsetZ);
            int layerIndexDefault = RhinoDoc.ActiveDoc.Layers.FindByFullPath("DEFAULT_LAYER", true);
            var rhinoObjects = RhinoDoc.ActiveDoc.Objects.FindByLayer(RhinoDoc.ActiveDoc.Layers[layerIndexDefault]);
            List<GeometryBase> listGeometryBases = new List<GeometryBase>();

            if (rhinoObjects == null || rhinoObjects.Length <= 0)
                return;

            foreach (var rhinoObject in rhinoObjects)
            {
                GeometryBase geometryBase = rhinoObject.Geometry;
                if (geometryBase == null)
                    continue;

                GeometryBase duplicatedGeometryBase = geometryBase.Duplicate();
                if (translation != Transform.Identity)
                    duplicatedGeometryBase.Transform(translation);
                listGeometryBases.Add(duplicatedGeometryBase);
            }
            //then add geometries to RhinoDoc from listGeometryBases
        }

The main problem is that i cant repeat the error in my computer… just to test it i’ve notice that if i put only the read file function in the Invoke of the rhino main window i have to put a RhinoApp.Wait(); to see the objects loaded using FindByLayer… but if i put inside the Invoke call all the code in the idle event i dont need to call to Wait()… so more questions comes in my head… do i have to put a RhinoApp.Wait() always to let rhino end his jobs? Is there anything that need to be done after reading a file?


Rhino crashing while trying to open a 3dm file on idle event
(Giulio Piacentino) #4

@Matteoz991I started a new thread. Please start new threads when you have new questions.