Close the istance of automated rhino



hi all, i have a C# application that automates rhino like this

            string rhinoId = "Rhino5x64.Application";
            System.Type type = System.Type.GetTypeFromProgID(rhinoId);
            rhino = System.Activator.CreateInstance(type);

but when it comes to close it there are some problem:
I can’t use rhino.Exit() because i get a runtime binder exception(method does not exist), i can’t use rhino.RunScript(_Exit, fasle) because it waits for an user input to choose wheter to save or not the edited file.

So how can i close rhino?


(Menno Deij - van Rijswijk) #2

It seems that the only reliable way is to enumerate the process before and after the call to Activator.CreateIntance and see which process is new. Then, you can selectively kill that process.


Thank you menno,

can’t we just disable the popup when exiting from rhino?

(Menno Deij - van Rijswijk) #4

Unfortunately, even in scripted mode, the Exit command pops up a dialog.


Maybe loop through your processes and search for Rhino4 or Rhino5x64 and kill that process?
Or do you need to save before closing?

(Menno Deij - van Rijswijk) #6

In that case, you run the risk of closing an instance that the user opened him/her self.


Thank you all for your responses.

I’m thinking to do this by another way: since my c# application is a multithreaded rhino automator( and i will have more than one istance of rhino), i will create a “garbage collector” that kills all the rhino process older than X.

(Dale Fugier) #8

Hi Alessio,

If you modify the document in anyway, you will be prompted to save. To suppress the “do you want to save” dialog, then get the RhinoScript object and call its DocumentModfied member. Here are some examples:

Here are some other C#, automation samples.

– Dale


thanks a lot dale!


Hi Dale,

Do you have an updated sample file for Rhino 6?
If I am automating opening a new session of Rhino, doing some operations, save and Close the session from my Grasshopper plugin, is this the right approach?


(Dale Fugier) #11

I don’t know - does it work?

– Dale



This is working until I try to load grasshopper from the script object.

 object obj=rhino.GetScriptObject();
 IRhinoScript script=(IRhinoScript) obj;
 dynamic gh=null;

It throws an exception while grasshopper is loading GHPython at startup and stops loading grasshopper:
An exception of type ‘System.Runtime.InteropServices.COMException’ occurred in System.Dynamic.dll but was not handled in user code
Additional information: The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))


(Dale Fugier) #13

Hi @AlanTai,

This probably isn’t enough information for me to help. Where is this code executiing? What the code, overall, do? Can you provide me a ‘simple’ example that I can run here that repeats this?

– Dale



I am working in my custom grasshopper component but, for example, in a grasshopper c# script component:

private void RunScript(bool x, ref object A)
    if(!x) return;
    dynamic rhinoApp = null;
      string rhinoId = "Rhino.Application";
      System.Type type = System.Type.GetTypeFromProgID(rhinoId);
      rhinoApp = System.Activator.CreateInstance(type);

    if (rhinoApp == null)
      Console.WriteLine("Failed to create Rhino application");

    const int bail_milliseconds = 15 * 1000;
    int time_waiting = 0;
    while (0 == rhinoApp.IsInitialized())
      time_waiting += 100;
      if (time_waiting > bail_milliseconds)
        Console.WriteLine("Rhino initialization failed");

    rhinoApp.Visible = 1;

    object obj = rhinoApp.GetScriptObject();
    IRhinoScript script = (IRhinoScript) obj;
    script.Print("test print");

    dynamic gh = null;
    gh = script.GetPlugInObject("Grasshopper");

From component output:

  1. The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) (line: 0)

(Dale Fugier) #15

Hi @AlanTai,

If I understand correctly, you are in Grasshopper, some custom C# component, and you want to launch another copy of Rhino and have it load Grasshopper? What is it you are trying to do an why?

– Dale



Yes, I am in grasshopper. I would like to automate running multiple other grasshopper definitions in a particular order (each grasshopper file has its own paired rhino worksession file) from my grasshopper component.