Opening and closing background process using Unity and ProcessStartInfo

1.2k Views Asked by At

I have been trying to write a quick script for my unity VR game to begin recording from an Azure Kinect camera at runtime, and close this script/recording on the close of application. To run their demo recording application, you run k4arecorder.exe output.mkv in a command prompt, and then pressing Ctrl-C to close the camera, which allows the program to save the file properly (see Azure Kinect SDK).

I have been able to successfully open the script and begin recording from the camera with the following code, but I have not been able to enter Ctrl-C (or any text at all) on the popup command prompt in order to stop the camera from running. And I can't use Process.StandardInput.Close() as I am running this with useShellExecute=True

The .exe saves the camera recording after Ctrl-C input, so I do need to close the stdin somehow before stopping the program entirely.

    ProcessStartInfo startInfo;
    Process process;

    void Start()
    {
        startInfo = new ProcessStartInfo();
        startInfo.FileName = @"C:\Program Files\Azure Kinect SDK v1.4.1\tools\k4arecorder.exe";
        startInfo.Arguments = System.IO.Directory.GetCurrentDirectory() + "\\output_testing.mkv";

        process = new Process {StartInfo = startInfo};
        process.Start();
    }

I've also tried a different approach, redirecting the StandardInput, but then I don't see any text at all on the popup command prompt, and my attempts to close stdin and print anything to the command prompt have also not worked. For example, I never see anything in the command prompt running:

ProcessStartInfo startInfo;
    Process process;
    void Start()
    {
        startInfo = new ProcessStartInfo();
        startInfo.FileName = @"C:\Program Files\Azure Kinect SDK v1.4.1\tools\k4arecorder.exe";
        startInfo.Arguments = System.IO.Directory.GetCurrentDirectory() + "\\output_testing.mkv";
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardInput = true;
        startInfo.CreateNoWindow = false;

        process = new Process {StartInfo = startInfo};
        process.Start();

        process.StandardInput.WriteLine("testing");
        string output = process.StandardOutput.ReadToEnd();
        UnityEngine.Debug.Log(output);
    }

    void OnApplicationQuit()
    {
        // this will just perpetually wait until I close the window cause cntrl-c not going thru
        process.StandardInput.Close();
        process.WaitForExit();
        process.Close();
    }

Any tips immensely appreciated! I feel like there must be a way past this with either method, but I'm coming up short. This is Unity 2019.4.1f1 and Azure Kinect SDK v1.4.1 (though I don't think this is a bug).

1

There are 1 best solutions below

0
On

Hi one thing that worked for me is using:

void OnApplicationQuit() { process.CloseMainWindow(); }