首页 > 解决方案 > 从 WPF 调用进程执行时间过长

问题描述

我有以下代码打开命令窗口(从 WPF 界面)并执行可能需要很长时间的代码,例如 @ 8-10 分钟:

ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = _exePath;
procStartInfo.Arguments = arguments;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;

using (Process pr = Process.Start(procStartInfo))
{
    pr.WaitForExit();
    string result = pr.StandardOutput.ReadToEnd();
    string[] split = result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

    int output = 0;
    int.TryParse(split[split.Length - 1], out output);

    return output;
}

Program.cs我有更新状态(显示操作状态和百分比)的方法:

Console.Title = "Loading ... 5 %";

// do request to server and check status

while(status.InProgress) {
    Thread.Sleep(2000); // 2 seconds
    status = web.getJsonFromApiServer(url); // {InProgress: "true", Message: "Checking X%";       
}

有时进程被挂起,并且它的标题不再更新,就像无限循环一样。

如果我使用控制台而不从 WPF 开始(我的意思是使用命令提示符,然后将位置设置为 exe 路径并使用参数运行它),它工作正常,没有问题。

为什么会发生这种事情?

标签: c#wpfconsole

解决方案


如果父进程在 p.StandardOutput.ReadToEnd 之前调用 p.WaitForExit 并且子进程写入足够的文本来填充重定向的流,则可能导致死锁情况。父进程将无限期地等待子进程退出。子进程将无限期地等待父进程从完整的 StandardOutput 流中读取。

为避免死锁,您应该使用异步方法:

var procStartInfo = new ProcessStartInfo
{
    FileName = _exePath,
    Arguments = arguments,
    UseShellExecute = false,
    CreateNoWindow = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
var p = new Process { StartInfo = procStartInfo };
p.OutputDataReceived += (sender, eventArgs) => { Console.WriteLine(eventArgs.Data); };
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();

推荐阅读