Search Unity

Start a external process

Discussion in 'Scripting' started by Mike08, Feb 3, 2009.

  1. Mike08

    Mike08

    Joined:
    Dec 29, 2005
    Posts:
    124
    Hi

    How can I start a comand line utility with Diagnostics.Process with 3 arguments and how can I get the standard output of the process?

    Thanks

    By
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Like this:
    Code (csharp):
    1. using UnityEngine;
    2. using System;
    3. using System.IO;
    4. using System.Diagnostics;
    5.  
    6. Process process = null;
    7. StreamWriter messageStream;
    8.  
    9. public class AppLauncher : MonoBehaviour
    10. {
    11.     void StartProcess()
    12.     {
    13.         try
    14.         {
    15.             process = new Process();
    16.             process.EnableRaisingEvents = false;
    17.             process.StartInfo.FileName = Application.dataPath + "/path/to/The.app/Contents/MacOS/The";
    18.             process.StartInfo.UseShellExecute = false;
    19.             process.StartInfo.RedirectStandardOutput = true;
    20.             process.StartInfo.RedirectStandardInput = true;
    21.             process.StartInfo.RedirectStandardError = true;
    22.             process.OutputDataReceived += new DataReceivedEventHandler( DataReceived );
    23.             process.ErrorDataReceived += new DataReceivedEventHandler( ErrorReceived );
    24.             process.Start();
    25.             process.BeginOutputReadLine();
    26.             messageStream = process.StandardInput;
    27.        
    28.             UnityEngine.Debug.Log( "Successfully launched app" );
    29.         }
    30.         catch( Exception e )
    31.         {
    32.             UnityEngine.Debug.LogError( "Unable to launch app: " + e.Message );
    33.         }
    34.     }
    35.  
    36.  
    37.     void DataReceived( object sender, DataReceivedEventArgs eventArgs )
    38.     {
    39.         // Handle it
    40.     }
    41.  
    42.  
    43.     void ErrorReceived( object sender, DataReceivedEventArgs eventArgs )
    44.     {
    45.         UnityEngine.Debug.LogError( eventArgs.Data );
    46.     }
    47.  
    48.  
    49.     void OnApplicationQuit()
    50.     {
    51.         if( process != null  !process.HasExited )
    52.         {
    53.             process.Kill();
    54.         }
    55.     }
    56. }
     
  3. Mike08

    Mike08

    Joined:
    Dec 29, 2005
    Posts:
    124
    Hi

    how can I send the 3 arguments to the process.

    When I try this code I get the error

    error CS0246: The type or namespace name `DataReceivedEventArgs' could not be found. Are you missing a using directive or an assembly reference?
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
  5. Mike08

    Mike08

    Joined:
    Dec 29, 2005
    Posts:
    124
    Hi

    I use it with Unity iPhone could this be the reason why he didn't find it.

    He doesn't find DataReceivedEventHandler and BeginOutputReadLine() too.

    What is wrong with this?

    By
     
  6. Mike08

    Mike08

    Joined:
    Dec 29, 2005
    Posts:
    124
    Hi

    How can I handle the arguments.

    I need to transfer 3 arguments to the program. But what ever I do it never gets the three arguments.

    (In my program I check if argc = 4 (the first is the startpath) )

    How can I do this?

    By
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    unity iPhone uses an older version of mono. Its possible that this version doesn't include those parts of the runtime.

    Regarding the arguments - it should just work like this:
    Code (csharp):
    1. process.StartInfo.FileName = Application.dataPath + "/path/to/The.app/Contents/MacOS/The index1 index2 index3";
     
  8. yson

    yson

    Joined:
    May 23, 2011
    Posts:
    7
    AngryAnt,

    Thank you so much! This really helped me make serial communication with threading.
     
  9. test12345

    test12345

    Joined:
    Mar 11, 2013
    Posts:
    5
    This works well, but as soon as you attach the monodevelop debugger to Unity, and try to hit a break point. Monodevelop and Unity both freeze because the debugger is hung up while waiting for input from your process. Once you kill your process, Monodevelop will hit your break point. Does anybody have a work around for this?
     
  10. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    if this is not an issue with the settings of the process object, maybe you could send regular empty data from your process (it could be only in you debug build for it..)
     
  11. chrisfdaqri

    chrisfdaqri

    Joined:
    Aug 5, 2015
    Posts:
    1
    This is great Emil thanks!
    I was seeing unity hang indefinitely trying to start a command line tool from unity editor on OSX, and this code fixes the hang. I think it might be the EnableRaisingEvents = false line as I wasn't do that before.
     
  12. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    It's working in editor when i use context menu
    but process not run when application is playing any idea about why
    thank's !
     
  13. Erveon

    Erveon

    Joined:
    Sep 15, 2019
    Posts:
    13
    For those stumbling upon this from Google; note that the posted solution does not work with IL2CPP and Unity is not planning to address this so you'll have to make your own. Closest solution would be this thread.
     
    LaireonGames likes this.