Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Process.Exited event works but functions do not call

Discussion in 'Windows' started by vagelis199, May 9, 2023.

  1. vagelis199

    vagelis199

    Joined:
    Jul 27, 2012
    Posts:
    184
    Hi, I have the following code:

    Code (CSharp):
    1.  void StartAC()
    2.     {
    3.  
    4.         GameRunning_UI.SetActive(true);
    5.         var GameProcess = Process.Start(new ProcessStartInfo
    6.         {
    7.             FileName = Acs_exe_path,
    8.             WorkingDirectory = AcRootDirectory,
    9.         });
    10.  
    11.         if (GameProcess == null || GameProcess.HasExited)
    12.         {
    13.             UnityEngine.Debug.LogError("Failed to start game process!");
    14.             return;
    15.         }
    16.         GameProcess.EnableRaisingEvents = true;
    17.         GameProcess.Exited += p_Exited;
    18.  
    19.         // Log some debugging information
    20.         UnityEngine.Debug.Log($"Game process started with ID {GameProcess.Id}");
    21.  
    22.     }
    23.     void p_Exited(object sender, EventArgs e)
    24.     {
    25.         GameExit();
    26.     }
    27.  
    28.     void GameExit(){
    29.         UnityEngine.Debug.Log("Game process exited!");
    30.         GameRunning_UI.SetActive(false);
    31.         Out_Script.GetResults();
    32.     }
    When I call StartAC() the GameRunning_UI gets activated the process starts, I get the log "Game process started with ID" in the console
    and when I exit the process, GameExit() seems to get called, I get "Game process exited!" in the console. But anything else after do not continue. GameRunning_UI never gets disabled and neither does the GetResults() get called.

    How I can solve this issue?
     
  2. vagelis199

    vagelis199

    Joined:
    Jul 27, 2012
    Posts:
    184
    A solution i found is to create a bool set it to true once p_Exited gets called, and run the functions I want in Update with if statement.
    Not sure if it's the best approach tho
     
  3. kscharpf

    kscharpf

    Joined:
    Apr 19, 2022
    Posts:
    1
    This is because p_Exited is being executed outside of the main thread and can not call call monobehavior or UI functions.

    The proper way to address this would be to create a dispatcher that queues up method calls on the next main thread update