sequence of operation doesn't wait for output of execution of open

edited October 2016 in Questions about Code

Ok so i run open("acquire.exe /scan ... ); - scans image into test.bmp then i run open("convert.exe test.bmp test.jpg"); - converts test.bmp to test.jpg

if i run code twice, second time it finds test.bmp and converts it to test.jpg, but complains it can't find it third time is a charm , runs and displays test.jpg because now its there.

So lines of code that call open and command line programs execute but apparently program doesn't wait for output before proceeding? Not used to this , in C code a call waits for return and then proceeds Is there a way to make the program wait till open returns before proceeding or something?

Answers

  • edited September 2015 Answer ✓

    According to open()'s reference: https://Processing.org/reference/open_.html

    It is equivalent to Runtime.getRuntime.exec(argv); if we pass a String[] array to it. http://docs.Oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String:A-

    From the link: "Executes the specified command and arguments in a separate process.".
    Therefore it doesn't "wait" for the external program to finish at all!

    Since open() returns a Process instance:
    http://docs.Oracle.com/javase/8/docs/api/java/lang/Process.html

    We have some control over it. Like invoking waitFor() to forcibly wait for the external program:
    http://docs.Oracle.com/javase/7/docs/api/java/lang/Process.html#waitFor();

    "Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated."

  • just getting back to this, what is the syntax to use waitFor() waitFor( open("program.exe /sw1 /sw2") ): get waitFor() function doesn't exist error

  • As it's clear from the links I've posted in my last reply, waitFor() method belongs to class Process.
    Therefore, in order to invoke it, we need a reference to a Process object 1st.

  • sorry, how do i do that, so open() is not a process , its a function that calls the process?

  • I understood open() returns a Process instance, so so i thought waitFor(open()); would work. I guess i don't understand, sorry.

  • edited September 2015

    Let's re-try step-by-step, like re-read my 1st re-ply, shall we?

    1. I said open() returns a Process object only when we pass a String[] array to it.
    2. Instead you pass "program.exe /sw1 /sw2". Which isn't inside an array!
    3. If we wish, we can directly chain-invoke waitFor() upon open()'s returning Process reference.
    4. Something like this: open(new String[] { "program.exe", "/sw1", "/sw2" }).waitFor();
    5. I guess it's gonna need a try/catch () block as well. :-<
  • Still trying to get this to work without invoking hard delay(x) after process open(strscan);

    Where strscan is the command line string for open() to use to start process of scanning in image.

    strscan=("c:/acquire/acquire.exe /SCAN/HIDE/GAMMA1.0/C0/GRAY"+strht+strwth+strdpi+strxoff+stryoff+" dirtscan.bmp");

    open(strscan);

    delay(35000);

    Above works, but delay value is dependent of area being scanned, resolution, etc. I want Draw to wait for the scan to complete and the file is written to the disk.

    Either by waiting for the process or checking for the file name that is written to the dick.

    I tryed open(new String[] {"c:/acquire/acquire.exe","/SCAN","/HIDE","/GAMMA1.0","/C0","/GRAY",strht,strwth,strdpi,strxoff,stryoff," dirtscan.bmp"}).waitFor();

    strht etc. are strings get error Unhandled exception

  • ... get error Unhandled Exception.

    That's my 5th recommendation: "I guess it's gonna need a try/catch () block as well."

  • @GoToLoop you're speaking in codes, man. Remember that not everyone is as experienced as you.

    What if you just had a function doOpen(String); that runs open() and delays the sketch until the file is open. (You would have to check for that somehow..) idk.

  • edited October 2016

    I don't have any experience at calling external programs.
    This is the most I can help you out.

    Again, Unhandled exceptions are compile errors for when a checked exception call isn't wrapped by a try / catch () block: https://Processing.org/reference/try.html

    It means that method waitFor() demands that:
    http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#waitFor()

Sign In or Register to comment.