c# - 从 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 路径并使用参数运行它),它工作正常,没有问题。
为什么会发生这种事情?
解决方案
如果父进程在 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();
推荐阅读
- objective-c - 可变参数函数中的错误“线程 1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)”
- salesforce - sfdx force:data:bulk:upsert 请求包含无效数据
- c# - “context.Request”引发了“System.Web.HttpException”类型的异常
- maven - maven 在 jar 中包含 testOutputDirectory
- python - 触发 Pi 相机时的多个图像实例
- django - 如何使用 Django 对视图设置站点范围的密码保护和权限要求?
- sql - Angular 和 Web API 基于令牌的身份验证 – 404 错误
- android - 如何在 android 中处理 TabLayout 中的重叠片段?
- jenkins-pipeline - Groovy - 无法在 JFrog Artifactory 中上传工件
- grafana - grafana中的温度单位转换