Hello,
I’ve been wrapping my head around calling custom python code from my C# Rhino plugin asynchronously.
To explain the setup, I want to launch from a C# Rhino Command some piece of python code that takes a lot of time to compute. meanwhile i’d like the Rhino to remain usable.When the code finishes to compute I’d like to trigger some C# code.
What I have tried :
-
Async function called by my command calling
string script_to_run = string.Format("_-RunPythonScript \"{0}\"", mainFile); await Task.Run(()=> RhinoApp.RunScript(script_to_run))
: problem observed is that RunScript does not seem to execute properly inside a Task, it returns false and does nothing. If this approach would work I would easily be able to do stuff AFTER the execution on the C# side, the only remaining problem would be passing data from and to python since RUnSCript doesn’t allow arguments for the python script and has no return. -
run on main thread RhinoApp.RunScript and start a thread on the python side for the expensive computations. This works pretty well but I have no way of knowing when the script ends on the C# side. Right now I am using UserStrings events and writing in userStrings from python to trigger the required code from C# side. Would it be possible to declare in the plugin a custom event that can be called from python side or to give python interpreter a callback that would be called on the main ui thread ?
Is there any other way than RhinoApp.RunScript to call python code using the Rhino python engine ?
Thanks for your ideas on this.