I'm having an issue where my program is stopped after I start up a bash process from my C# code, but only on the second time, the first time it runs correctly.
Here is some sample code I have written to recreate the issue, you will notice that I kill and dispose the initial process object before starting the second one:
public class Program
{
public static async Task MainAsync(string[] args)
{
// Start first instance of process
GstProcess gstProcess = new GstProcess();
await gstProcess.Execute();
await Task.Delay(5000);
// Start second instance of process after waiting 5 seconds
gstProcess = new GstProcess();
await gstProcess.Execute();
}
class GstProcess
{
Process? gstProcess;
string audioDevice;
public GstProcess()
{
audioDevice = AudioDevice.GetUsbAudioOutDeviceName();
}
public async Task Execute()
{
StartProcess();
await Task.Delay(5000);
await StopProcess();
}
void StartProcess()
{
if (gstProcess == null)
{
string pipelineCommand = $"gst-launch-1.0 audiotestsrc ! audioconvert ! volume volume=0.1 ! audioresample ! pulsesink device={audioDevice}";
gstProcess = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "/bin/bash",
Arguments = $"-c -i \"{pipelineCommand}\"",
RedirectStandardInput = false,
RedirectStandardOutput = false,
RedirectStandardError = false,
UseShellExecute = false
}
};
Console.WriteLine($"Running Command: {pipelineCommand}");
gstProcess.Start();
Console.WriteLine($"Process Started");
}
}
async Task StopProcess()
{
Console.WriteLine("Shutting down gstreamer process");
if (gstProcess != null)
try
{
gstProcess.Kill();
await gstProcess.WaitForExitAsync();
}
finally
{
gstProcess.Dispose();
gstProcess = null;
Console.WriteLine("Process was shut down");
}
}
}
}
Here is the output of my program, you can see that the program is stopped immediately after the process starts for the second time.
Does anyone know why this is occurring?

It could be some resource is locked by the first run.
Calling Kill on a process doesn't allow it to close gracefully.
The process doesn't have time to do any cleanup, like closing file or other hardware handles.
There should be a "Close" command to tell the command line app to shut itself down.
It may be another command line parameter that tells the program to shutdown after running the command.