Search Unity

Help needed using System.Diagnostics.Process

Discussion in 'macOS' started by NGC6543, Jan 19, 2018.

  1. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    Hi, I have an external app that needed to be run when the main app starts.
    I tried to use System.Diagnostics.Process, but I couldn't achieved what I wanted it to be.

    My goal is :

    1. When the main app starts, it first looks for a running process that has the specific name.
    2. If there is no running process then create a new Process, set EnableRaisingEvents to true, add event listener to Process.Exited the start it.
    3. If there is a running process, then save the process reference to my private Process field, set EnableRaisingEvents to true, add event listener to Process.Exited.

    Here is the code snippet :

    Code (CSharp):
    1.  
    2. public void InitSensor (bool _killRunningProcess = true)
    3.         {
    4.             status = MyApp_STATUS.OFF;
    5.  
    6.             Process[] processes = Process.GetProcesses();
    7.             for (int i = 0; i < processes.Length; i++)
    8.             {
    9.                 try
    10.                 {
    11.                     if (!processes[i].HasExited)
    12.                     {
    13.                         if(processes[i].ProcessName.StartsWith("MyApp", StringComparison.InvariantCulture))
    14.                         {
    15.                             //HACK should not kill and attach to the running process??
    16.                             if (_killRunningProcess)
    17.                             {
    18.                                 UnityEngine.Debug.Log("Running " + processes[i].ProcessName + " found! Restarting...");
    19.                                 processes[i].Kill();
    20.                                 status = MyApp_STATUS.EXITED;
    21.                             }
    22.                             else
    23.                             {
    24.                                 //HACK Exited is invoked. don't know why!!!!
    25.                                 UnityEngine.Debug.Log("Running " + processes[i].ProcessName + " found! Attaching...");
    26.                                 pSensor = processes[i];
    27.                                 pSensor.EnableRaisingEvents = true;
    28.                                 pSensor.Exited += OnSensorTerminated;
    29.                                 status = MyApp_STATUS.RUNNING;
    30.                             }
    31.                             //UnityEngine.Debug.Log("Running " + processes[i].ProcessName + " found! Restarting...");
    32.                             ////HACK not working. Don't know why
    33.                             //processes[i].Kill();
    34.                             //status = MyApp_STATUS.EXITED;
    35.                             return;
    36.                         }
    37.                     }
    38.                 }
    39.                 catch (Exception e)
    40.                 {
    41.                     //UnityEngine.Debug.LogWarning(e);
    42.                 }
    43.             }
    44.  
    45.             //=== No running MyApp found. Start one.
    46.             status = MyApp_STATUS.INITIALIZING;
    47.  
    48.             //TODO Check if pSensor != null
    49.             if (pSensor != null)
    50.             {
    51.                 pSensor.Close();
    52.                 pSensor = null;
    53.             }
    54.  
    55.             pSensor = new Process();
    56.             switch (hardware)
    57.             {
    58.                 case MyApp_HARDWARE.Sensor_V1 :
    59.                     UnityEngine.Debug.Log("Starting MyApp for Sensor V1...");
    60.                     pSensor.StartInfo.FileName = Application.streamingAssetsPath + "/MyAppSensors/MyApp_Sensor_V1.app";
    61.                     break;
    62.                 case MyApp_HARDWARE.Sensor_V2 :
    63.                     UnityEngine.Debug.Log("Starting MyApp for Sensor V2...");
    64.                     pSensor.StartInfo.FileName = Application.streamingAssetsPath + "/MyAppSensors/MyApp_Sensor_V2.app";
    65.                     break;
    66.                 default :
    67.                     UnityEngine.Debug.Log("Starting MyApp for Sensor V1...");
    68.                     pSensor.StartInfo.FileName = Application.streamingAssetsPath + "/MyAppSensors/MyApp_Sensor_V1.app";
    69.                     break;
    70.                
    71.             }
    72.             pSensor.StartInfo.CreateNoWindow = false;
    73.             pSensor.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    74.             pSensor.EnableRaisingEvents = true;
    75.             pSensor.Exited += OnSensorTerminated;
    76.  
    77.             pSensor.Start();
    78.             //status = MyApp_STATUS.RUNNING; // RUNNING is set when the first data has arrived
    79.         }
    80.  
    81.         /// <summary>
    82.         /// Explicitly terminates the sensor.
    83.         /// </summary>
    84.         public void KillSensor()
    85.         {
    86.             if (pSensor != null)
    87.             {
    88.                 //UnityEngine.Debug.Log("Terminating MyApp...");
    89.                 status = MyApp_STATUS.OFF;
    90.                 if (!pSensor.HasExited)
    91.                 {
    92.                     pSensor.Exited -= OnSensorTerminated;
    93.                     try
    94.                     {
    95.                         //HACK not working... don't know why
    96.                         pSensor.Kill();
    97.                     }
    98.                     catch (Exception e)
    99.                     {
    100.                         UnityEngine.Debug.LogWarning(e);
    101.                     }
    102.                 }
    103.                 pSensor.Close();
    104.                 pSensor = null;
    105.  
    106.                 //HACK pSensor.Kill() not working.. have to implement other way
    107.                 Process[] processes = Process.GetProcesses();
    108.                 for (int i = 0; i < processes.Length; i++)
    109.                 {
    110.                     try
    111.                     {
    112.                         if (!processes[i].HasExited)
    113.                         {
    114.                             if(processes[i].ProcessName.StartsWith("MyApp", StringComparison.InvariantCulture))
    115.                             {
    116.                                 UnityEngine.Debug.Log("Terminating " + processes[i].ProcessName + "...");
    117.                                 processes[i].Kill();
    118.                                 status = MyApp_STATUS.OFF;
    119.                                 return;
    120.                             }
    121.                         }
    122.                     }
    123.                     catch (Exception e)
    124.                     {
    125.                         //UnityEngine.Debug.LogWarning(e);
    126.                     }
    127.                 }
    128.             }
    129.             else
    130.             {
    131.                 UnityEngine.Debug.Log("MyApp is not running!");
    132.             }
    133.         }
    And the problem is, I can't attach the running process to my local field. The process is assigned to pSensor, but calling pSensor.Kill() has no effect. If I do the same job by calling
    Code (CSharp):
    1. processes[i].Kill()
    ,
    then it works.

    And if i attach the running process, then pSensor.Exited event is invoked right after assigning it / or never raised when I terminate the external sensor app. If I force stop the running process and create a new process inside my app, then the events are correctly raised.

    I tried to search online documents and use examples, but couldn't find useful one. If anyone can help, it would be very grateful.
     
    Last edited: Jan 19, 2018