Rhino 8 Layer Viewer crashes while generating layers on seperate Thread

Hello,

we are developing a plugin with WPF UI. Some functions are executed on a seperated thread and since our upgrade to Rhino 8 we experiencing a bug, where the UI freezes during adding and removing layers.

This is how our plugin should behave. In Rhino 7 everything works fine:

expected

With Rhino 8 the Layer View Panel freezes when we start our commands:

Rhino8

I uploaded a the sample projects from the pictures above, which mimics some basic functionalities of our plugin: wegewitzSte/RhinoLayerError_demonstration (github.com)

Here you see the code, which causes the error:

 protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // Start a background task
            Task.Run(() =>
            {
                doc.Views.RedrawEnabled = false;

                for ( int i = 0; i < 10; i++ )
                {
                    var layername = $"Building::{i}_{System.Guid.NewGuid()}";

                    // own version of adding a layer by path parten::child::etc...
                    int layerIndex = stuff.addLayerByFullPath(doc, layername.ToString(), System.Drawing.Color.DarkBlue);
                }
                doc.Views.RedrawEnabled = true;
                doc.Views.Redraw();
            });

            return Result.Success;
        }

Using a backgroundtask in Rhino 7 seems to work fine, but in Rhino 8 not.

I found a quick fix by using the RhinoUI Thread by using RhinoApp.InvokeOnUiThread , which seems to fix it in Rhino 8:

 protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {

            // Start a background task
            Task.Run(() =>
            {
                RhinoApp.InvokeOnUiThread((System.Action) (() =>
                {

                    doc.Views.RedrawEnabled = false;

                    for ( int i = 0; i < 10; i++ )
                    {
                        var layername = $"Building::{i}_{System.Guid.NewGuid()}";

                        // own version of adding a layer by path parten::child::etc...
                        int layerIndex = stuff.addLayerByFullPath(doc, layername.ToString(), System.Drawing.Color.DarkBlue);
                    }


                    doc.Views.RedrawEnabled = true;
                    doc.Views.Redraw();
                }));

            });

            return Result.Success;
        }

Currently this fix is working for us, since we could isolate the function which are changing things with the layers. But iā€™m questioning if this is working as intended.

Is there maybe a better fix or could this be fixed with newer Rhino 8 versions?

Thank You!

I think we have the same problem - see Rhino 8.9 layer table window not updating and crashing - Rhino Developer - McNeel Forum ?

We found from Rhino 8.9 the LayerTable Viewer is significiently less forgiving of work on the background thread & in Rhino 7 we also did not have an issue.

Thanks for the hint!

Whatever you do: NEVER make changes to the Rhino document on a different thread than the main thread. Document modification is not thread safe, it never has been. I guess you got lucky that it worked in Rhino 7.

1 Like