You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#程序第二次启动bash进程后异常终止问题求助

C#启动bash进程首次正常,第二次启动后程序直接停止

我用C#启动bash进程时,第一次运行完全正常,但第二次启动进程后程序会立刻停止。我已经在启动第二个进程前销毁并释放了第一个进程对象,以下是复现问题的示例代码:

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");
                }
        }
    }
}

程序输出显示第二次启动进程后立即停止,截图如下:
程序第二次启动后的输出

请问这是什么原因?

内容的提问来源于stack exchange,提问作者Cameron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:25:27