我有一个WPF应用程序,在处理文件时需要使用docker进程。 docker容器构建在盒子上,目前在使用WPF应用程序处理文件后,用户必须启动命令提示符并输入
docker run --it --rm -v folderdedirect process parameters_including_filePath
进行进一步处理。
我想在WPF应用程序中包含它。我可能会将system.diagnostics.process
与cmd.exe
一起使用?我查看了Docker.dotnet
,但是我的生活无法理解它应该如何运行本地容器。
答案 0 :(得分:0)
这是我最后的操作方式,但是可能会有更好的方法。
var processInfo = new ProcessStartInfo("docker", $"run -it --rm blahblahblah");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
int exitCode;
using (var process = new Process())
{
process.StartInfo = processInfo;
process.OutputDataReceived += new DataReceivedEventHandler(logOrWhatever());
process.ErrorDataReceived += new DataReceivedEventHandler(logOrWhatever());
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(1200000);
if (!process.HasExited)
{
process.Kill();
}
exitCode = process.ExitCode;
process.Close();
}
答案 1 :(得分:-1)
使以上代码适合我的上下文。最难的部分是确定过程完成后如何监视。您可以尝试设置事件监听器,如here所示。有其他条件要验证(如果客户端发送了终止信号),我决定继续监视流程。HasExited值。
private static void RunDockerImage(ContainerData containerData)
{
var processInfo = new ProcessStartInfo("docker", $"run "+containerData.ImageName);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
int exitCode;
using (var process = new Process())
{
process.StartInfo = processInfo;
// indicate process is started
StartUpdateOrch(containerData);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
while(!process.HasExited)
{
// continually check if kill signal is set
if (getKillSignal())
{
process.Kill();
}
Thread.Sleep(2000);
}
exitCode = process.ExitCode;
containerData.exitCode = exitCode;
// indicate process is done
FinishUpdateOrch(containerData);
process.Close();
}
}