Issue with Running Rhino Commands in a Custom C# Server via RunScript

Hello !

I’ve encountered a problem when trying to execute Rhino commands using the RhinoApp.RunScript method within a custom C# server. To give some context, I’ve created a custom Rhino plugin that includes a server implemented in C#. This server listens for specific commands, such as moving vertices, which are then executed within Rhino. The server is launched using a custom command StartServer, derived from Rhino.Commands.Command.

I’ve created a custom Rhino plugin that starts a C# server using the StartServer command, which runs on a separate thread. The server receives commands, like moving vertices, and executes them in Rhino using RhinoApp.RunScript. However, when these commands are executed, Rhino processes the script but doesn’t make any visible changes to the document, likely due to threading issues. It seems Rhino commands executed from a background thread don’t properly sync with the main UI thread. I’m seeking advice on how to properly execute and synchronize these commands in a multi-threaded environment to ensure they work as expected.

Here’s the code structure for starting the server:
‘’’
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Rhino;
using Rhino.Commands;
using Rhino.Display;

namespace PlugIn_Modelisation
{
[CommandStyle(Style.ScriptRunner)]
public class StartServer : Command
{
private static BackgroundWorker _worker;
public StartServer()
{
Instance = this;
}

    public static StartServer Instance { get; private set; }

    public override string EnglishName => "StartServer";

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
        RhinoView activeView = doc.Views.ActiveView;
        var shadedMode = DisplayModeDescription.FindByName("Shaded");
        activeView.ActiveViewport.DisplayMode = shadedMode;
        activeView.Redraw();

        Server serveur = Server.build();

        // The issue seems to be related to this part:
        Thread newThread = new Thread(() => serveur.RunServer(doc));
        newThread.Start();

        RhinoApp.WriteLine("Server done");

        return Result.Success;
    }
}

}
‘’’