在reader.ReadLine()之后进程窗口不能继续

我使用Process对接Neo4j Docker镜像。 在执行操作之前,我需要确保图像正确对接。 正如你在这里看到的,我将Docker Toolbox的标准输出redirect到Process窗口,然后写入Docker Toolbox正在做的任何事情。 但是,在图像停靠之后,根本不会进行并停留在该状态。 除了while循环之外的所有代码都不会被执行。

 ProcessStartInfo psi = new ProcessStartInfo(); psi.WindowStyle = ProcessWindowStyle.Normal; psi.FileName = ConfigurationManager.AppSettings["Bash"]; psi.WorkingDirectory = ConfigurationManager.AppSettings["ToolBox"]; psi.Arguments = BuildArgumentString(); psi.UseShellExecute = false;//set to false to redirect standard output psi.RedirectStandardOutput = true; Process process = Process.Start(psi); StreamReader reader = process.StandardOutput; while (!reader.EndOfStream) { Console.WriteLine(reader.ReadLine()); } //codes beyond this while loop is not executed 

这是过程窗口。

在这里输入图像说明

您的容器交互式运行,不分离。 循环将不返回到主程序,因为它正在等待stream的结束 – 但只要容器的运行它将被连接到stdinstdout ,并且stream不会结束。

Docker CLI通过向Docker引擎的REST API发送命令来工作。 如果要通过代码pipe理容器,最好绕过CLI并直接使用API​​ – Docker.DotNet项目为API提供.NET包装器。