Grasshopper

algorithmic modeling for Rhino

Hey guys,

This should be simple but I can't quite piece it together. I would like to call an executable from within a Grasshopper script (no arguments), and then wait for it to exit before the code continues. Normally I would do something along the lines of...

Process myProcess = new Process();

myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.WorkingDirectory = directory;
myProcess.StartInfo.FileName = analysisExec;
myProcess.Start();
myProcess.WaitForExit();

However this isn't currently possible since within a GH script we cannot currently change the directives (i.e. would need to add using System.Diagnostics). I've tried to instead use

var process = System.Diagnostics.Process.Start(analysisExec);
process.WaitForExit();

to bypass the problem, but a command window simply pops up briefly and then disappears (the analysis code will take quite a while). Any other ways to make this happen?

Thanks,

Austin

Views: 1421

Replies to This Discussion

Hi Austin

the way you are suggesting (using the full type name) should work. If a window appears, it means that it the call is working. What else could be going wrong? Could you run a "batch process" (.bat) that contains just a "pause" instruction, so that you would know if it is the WaitForExit call that is not working?

Just a thought. There are several cases where other people got this to work.

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

That is a good troubleshooting tip, thank you! Said pause.bat file is functioning properly and the window is staying up until I give it a keystroke, as we'd expect from WaitForExit. So I guess the issue must be with the analysis executable? Very strange. If I just run the executable from the command line it runs properly.

Just to double-check...the analysisExec string should simply be "C:\Users\...\analysis.exe", correct? Not something strange like ""C:\Users\...>analysis.exe"?

EDIT: Okay I tried just using the batch file to call the executable with a PAUSE command afterwards to keep the command window up. I've attached a pic of what's going on. So I guess the process directory isn't being properly defined?

EDIT2: Yup, bingo. Needed to set the working directory. In case anybody else runs up against this issue, the full executable call should be:

Directory.SetCurrentDirectory(codeSetup);

var process = System.Diagnostics.Process.Start(analysisExec);

process.WaitForExit();

Thanks for giving me the troubleshooting tool I needed, Giulio!

Great Austin -- just remember to set it back to what it was. It might have unintended consequences for other scripts or processes.

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service