2 Replies Latest reply on Aug 6, 2010 8:28 AM by Turalyon2006

    Can AIR wait/pause/hang/sleep/delay/join until a NativeProcess exits?

    Turalyon2006

      Hello!

       

           I am currently working on an Adobe AIR + Javascript application that reads in text files (where the text is in a certain format) and processes the contents into tokens and eventually into JSON. The JSON tokens are then fed to an instance of the YUI treeview. One feature of the program is that it is able to read in a token containing "name@DB", use a NativeProcess instance to execute the curl command from the Command Line Terminal, which fetches an existing JSON object from an online database. My code for handling such a token looks like this:

       

      CODE:

       

      this.tokenize = function(contents){

           var toks = [];

           toks = contents.split("\n");

           for (var i = 0; i < toks.length; ++ i){

                //Additional token cases are checked before @DB case

       

                else if (toks[i].indexOf("@DB") >= 0){   
                     type = "DB Reference";

       

                      //Custom function object that stores the name, value, source file, parent Token, and Token type.

                      //This allows for the assembly of a JSON token.

                      var tok = new Token(name, value, this.name, parent, type);


                      //Function to create and execute Native Process.

                      //See below for code.

                      this.fetch(tok);


                      //Array that stores the current files tokens

                      this.tokens.push(tok);

                }

                //More token processing

           }

      }

       

      //FETCH FUNCTION

      this.fetch = function(token){

       

         //Name of object to fetch from database
         var name = token.getVal().slice(token.getVal().indexOf(":") + 1, token.getVal().indexOf("@"));


         if(air.NativeProcess.isSupported)
         {
            try
            {

               //air.NativeProcess setup
               var executable = new air.File("/usr/bin/curl");
               var nativeProcessStartupInfo = new air.NativeProcessStartupInfo();
                  
               var processArgs = new runtime.Vector["<String>"]();
               processArgs.push("-k");

       

               processArgs.push("https://<URL>/fetch/" + name + "/json");

       

               nativeProcessStartupInfo.arguments = processArgs;
                   
               nativeProcessStartupInfo.executable = executable;
               var process = new air.NativeProcess();  

       

       

               //EventListener on STDOUT

               process.addEventListener(air.ProgressEvent.STANDARD_OUTPUT_DATA, onDataOut);

       

               //EventListener on STDERR
               process.addEventListener(air.ProgressEvent.STANDARD_ERROR_DATA, onErrorOut);
                      
               process.start(nativeProcessStartupInfo);

       

               //Wait until process exits?


               //Event handler for STDOUT

               function onDataOut(){

       

                  //Reads in the reply from the database
                  var returnStr = String(process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));

       

                  //Event listener for when the reply from the database has been processed.

                  Commander.dispatcher.addEventListener("DBDone", function(){this.setDBDone(returnStr, token);});

       

                  //Tokenize database reply and add the tokens to the token array

                  //Dispatches custom air.Event of type="DBDone"

                  appendDBTokens(returnStr);
              }

              //Event handler for STDERR
              function onErrorOut(){

       

                 //Read in the error message
                 var str = String(process.standardError.readUTFBytes(process.standardError.bytesAvailable));

       

                 //Append error message to error message display field
                 $("sysOut").value += "Error : " + str + "\n";


              }
           }
           catch(err)
           {
              alert("Error: " + err);
           }
        }
        else
           alert("NativeProcess is not supported!");

      };

       

      QUESTION:

       

      Is there a way for Adobe AIR to wait (I.E. Not execute anymore code) until the NativeProcess "process" exits?

       

      ADDITIONAL INFO:

       

      The reason I ask is because currently, while the event listeners are waiting for the reply from the database, fetch(token) returns and the for-loop in tokenize(contents) continues to process the rest of the tokens. The first problem with this is that the tokens are now out of order, the second problem is that once tokenize(contents) exits, then the file's tokens are fed to the YUI treeview instance and the tree is built, all before the database reply makes it back. So the tree is being built without the additional tokens from the database. If there is a waitForExit() or some equivalent function, I have thus far been unable to find it in the AIR API Reference materials.

       

      SYSTEM/PROGRAM INFORMATION:

       

      OS: RedHat Linux

      Languages Used: Javascript, HTML

      Additional Libraries: YUI, Adobe AIR

       

      Thanks, in advance!