RunScript and threads

Hi
I have second threads that do some jobs in my plugin. Some of these threads must call RunScript (“SaveAs”) , RunScript(“Open”), RunScript(“Save”), RunScript(“Import”) , and then continue with their job after the script is executed. But since these RunScript commands must be called in the main thread, I schedule them to execute in the main thread and make the second thread wait with a lock until the main thread signalizes that it is done.

So the pesudo code is basically this

secondThread()
{
    run some code in second thread
    shedule code for executing in main thread ( that is the code that calls RunScript)
    wait with lock  until the main thread completes 
    continue with executing code in second thread
} 

mainThreadCode()
{
     bool rslt = RhinoApp().ActiveDoc().RunScript("SaveAs");    
     notify second thread to continue 
}

The problem is that I notify the second thread to continue after RunScript is called. But after call to RunScript is completed it does not mean that the SaveAs command is actually completed. Question is how to signal the second thread to continue when the saveas command is really completed.

I hope my question is clear
Thank you

Hi @Elizabeta,

Just before you start scripting a file i/o operation, set some flag that can be watched by an “OnIdle” event handler

http://developer.rhino3d.com/api/cpp/class_c_rhino_is_idle.html

The the idle event handle is called, check to see if the flag is set. If so, clear the flag and resume your thread.

Here is an example of an “OnIdle” handler.

https://github.com/mcneel/rhino-developer-samples/blob/5/cpp/SampleCommands/cmdSampleIdleProcessor.cpp

Does this help?

– Dale

Just to be sure that I understand you. I should enable the onidle event just before calling RunScript(“SaveAs”) , then when the OnIdle event is hit for the first time then the Saveas command is already completed and it is safe to signal the second thread to continue, right ?

Hi @Elizabeta,

You can instantiate the event handler any time.

When the event handler is called, you will know it was you triggering the SaveAs operation because you will have set a flag indicating such before scripting the command.

– Dale