I’ve build a Unity application which launches other projects, such as games. I’m starting those other applications via the C# Process class. Next, I need a callback to know, when the new running process was closed, so that I can perform some UI actions in my main application.
public void RunProject(string path)
{
try
{
Process p = Process.Start(path);
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(ProcessExited2);
}
catch(Exception e)
{
///
}
}
private void ProcessExited2(object sender, EventArgs e)
{
Process p = (Process)sender;
// Dispose of resources held by the process.
p.Close();
RunningInstance = null;
if(OnProcessExited != null)
OnProcessExited.Invoke();
}
The above code practically works. I starts another Unity game and when it is closed, OnProcessExited is invoked. However, I guess because Unity isn’t thread safe, I get an exception:
“get_isPlaying can only be called from the main thread.”
I don’t use Application.isPlaying anywhere in my project, so I assume it is called by the new running instance I started via the Process class. I can see that there is a problem, because I have two open Unity processes on separate threads and one is sending a callback to the other, which is not allowed, because the Unity API is not thread safe.
Is there any way to go about this in a better way? Or should this theoretically work and I’m making some other mistake?
Thanks!