# Class: oProcess

## $. oProcess

Process class that allows user to launch executables outside harmony and get feedback from them.

#### new $.oProcess(bin, queryArgs)

openHarmony/openHarmony_threading.js, line 356
  The constructor for $.oProcess.

  Name | Type | Description
  --- | --- | ---
  `bin` | string | The path to the binary executable that will be launched.
  `queryArgs` | Array.<string> | A string array of the different arguments given to the command.

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `process` | QProcess | the QProcess object wrapped by the $.oProcess object.
  `bin` | string | The path to the binary executable that will be launched.
  `queryArgs` | Array.<string> | A string array of the different arguments given to the command.

### Members

#### readChannelstring

  Which channel will the process read from. Set before launching the process. can take the values "All", "Output" and "Error".

### Methods

#### execute(){string}

openHarmony/openHarmony_threading.js, line 540
  Execute a process and waits for the end of the execution.

  ##### Returns:

  Type | Description
  --- | ---
  string | The lines as returned by the process.

#### launchAndDetach()

openHarmony/openHarmony_threading.js, line 555
  Execute a process as a separate application, which doesn't block the script execution and stops the script from interacting with it further.

#### launchAndRead(readCallback, finishedCallback)

openHarmony/openHarmony_threading.js, line 488
  Execute a process and read the result as a string.

  Name | Type | Description
  --- | --- | ---
  `readCallback` | function | optional User can provide a function to execute when new info can be read. This function's first argument will contain the available output from the process.
  `finishedCallback` | function | optional User can provide a function to execute when new process has finished

  ##### Example

      // This example from the openHarmony oScene.renderWriteNodes() function code
      // uses the oProcess class to launch an async process and print its progress
      // to the MessageLog.

      // declaring the binary called by the process
      var harmonyBin = specialFolders.bin+"/HarmonyPremium";

      // building the list of arguments based on user provided input
      var args = ["-batch", "-frames", startFrame, endFrame, "-res", resX, resY];

      // different arguments depending on wether the scene is stored on the database or offline
      if (this.online){
        args.push("-env");
        args.push(this.environnement);
        args.push("-job");
        args.push(this.job);
        args.push("-scene");
        args.push(this.name);
      }else{
        args.push(this.stage);
      }

      // Create the process with the arguments above
      var p = new this.$.oProcess(harmonyBin, args);
      p.readChannel = "All"; // specifying which channel of the process we will listen to: here we listen to both stdout and error.

      // creating an async process
      if (renderInBackground){
        var length = endFrame - startFrame;

        // Creating a function to respond to new readable information on the output channel.
        // This function takes a "message" argument which will contain the returned output of the process.

        var progressDialogue = new this.$.oProgressDialog("Rendering : ",length,"Render Write Nodes", true);
        var self = this;

        var renderProgress = function(message){
          // parsing the message to find a Rendered frame number.
          var progressRegex = /Rendered Frame ([0-9]+)/igm;
          var matches = [];
          while (match = progressRegex.exec(message)) {
            matches.push(match[1]);
          }
          if (matches.length!=0){
            // if a number is found, we compare it to the total frames in the render to deduce a completion percentage.
            var progress = parseInt(matches.pop(),10)
            progressDialogue.label = "Rendering Frame: "+progress+"/"+length
            progressDialogue.value = progress;
            var percentage = Math.round(progress/length*100);
            self.$.log("render : "+percentage+"% complete");
          }
        }

        // Creating a function that will trigger when process exits.
        // This function can take an "exit code" argument that will tell if the process terminated without problem.

        var renderFinished = function(exitCode){
          // here we simply output that the render completed successfully.
          progressDialogue.label = "Rendering Finished"
             progressDialogue.value = length;
          self.$.log(exitCode+" : render finished");
        }

        // launching the process in async mode by providing true as first argument, and then the functions created above.

        p.launchAndRead(renderProgress, renderFinished);
        this.$.log("Starting render of scene "+this.name);
      }else{

        // if we don't want to use an async process and prefer to freeze the execution while waiting, we can simply call:
        var readout  = p.execute();
      }

      // we return the output of the process in case we didn't use async.
      return readout

#### read(){string}

openHarmony/openHarmony_threading.js, line 520
  read the output of a process.

  ##### Returns:

  Type | Description
  --- | ---
  string | The lines as returned by the process since the last "read" instruction