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

WinForms调用嵌套CLI进程,如何实现实时输出到ListBox?

实时捕获嵌套CLI进程输出的问题

我开发了一款C#命令行应用(CLI)SimSwitcher.exe,现需为其创建GUI版本。设计思路是让用户选择配置项生成CLI调用参数,通过Process启动该CLI,并将其输出实时显示在Form的ListBox控件(outputBox)中。尝试过多种常规方案,但均未解决核心问题。

核心实现代码

启动CLI的代码:

ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
// FileName and Arguments are specific to my CLI
info.FileName = "SimSwitcher.exe";
info.Arguments = String.Format("{0} -d {1} -f1 {2} -f2 {3} -s {4} -l {5}", command.ToString(), device.ToString(), file1Path, file2Path,
            Convert.ToString(this.startingAddress, 16), Convert.ToString(this.length, 16));
info.RedirectStandardOutput = true;
proc = new Process();
proc.StartInfo = info;
proc.EnableRaisingEvents = true;
proc.OutputDataReceived += Proc_OutputDataReceived;
proc.Start();
proc.BeginOutputReadLine();

输出捕获的事件处理代码:

private void Proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        this.BeginInvoke(new MethodInvoker(() => { outputBox.Items.Add(e.Data);}));
    }
}

现有问题

当前实现无法做到真正的实时输出:SimSwitcher.exe内部会依次调用三个第三方CLI进程,实际情况是第一个进程的输出要等到该进程结束后才会添加到ListBox,第二、第三个进程也是如此。

我曾在SimSwitcher.exe中使用类似代码启动这三个进程,但不仅未解决问题,反而导致SimSwitcher.exe在命令行运行时无法实时显示输出(此前是可以的)。怀疑问题出在这三个第三方CLI进程上,但不确定是否存在遗漏,恳请提供反馈或解决方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 14:51:03