【发布时间】:2020-02-13 13:11:24
【问题描述】:
我的进程在等待退出时挂起的原因可能是什么?
此代码必须启动内部执行许多操作的 powershell 脚本,例如通过 MSBuild 开始重新编译代码,但问题可能是它生成过多的输出,并且即使在 power shell 脚本正确执行后,此代码在等待退出时也会卡住
这有点“奇怪”,因为有时这段代码运行良好,但有时却卡住了。
代码挂在:
process.WaitForExit(ProcessTimeOutMiliseconds);
Powershell 脚本在 1-2 秒内执行,而超时时间为 19 秒。
public static (bool Success, string Logs) ExecuteScript(string path, int ProcessTimeOutMiliseconds, params string[] args)
{
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (var outputWaitHandle = new AutoResetEvent(false))
using (var errorWaitHandle = new AutoResetEvent(false))
{
try
{
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "powershell.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Arguments = $"-ExecutionPolicy Bypass -File \"{path}\"",
WorkingDirectory = Path.GetDirectoryName(path)
};
if (args.Length > 0)
{
var arguments = string.Join(" ", args.Select(x => $"\"{x}\""));
process.StartInfo.Arguments += $" {arguments}";
}
output.AppendLine($"args:'{process.StartInfo.Arguments}'");
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(ProcessTimeOutMiliseconds);
var logs = output + Environment.NewLine + error;
return process.ExitCode == 0 ? (true, logs) : (false, logs);
}
}
finally
{
outputWaitHandle.WaitOne(ProcessTimeOutMiliseconds);
errorWaitHandle.WaitOne(ProcessTimeOutMiliseconds);
}
}
}
脚本:
start-process $args[0] App.csproj -Wait -NoNewWindow
[string]$sourceDirectory = "\bin\Debug\*"
[int]$count = (dir $sourceDirectory | measure).Count;
If ($count -eq 0)
{
exit 1;
}
Else
{
exit 0;
}
在哪里
$args[0] = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe"
编辑
在@ingen 的解决方案中,我添加了一个小包装器,它会重试执行挂起的 MS Build
public static void ExecuteScriptRx(string path, int processTimeOutMilliseconds, out string logs, out bool success, params string[] args)
{
var current = 0;
int attempts_count = 5;
bool _local_success = false;
string _local_logs = "";
while (attempts_count > 0 && _local_success == false)
{
Console.WriteLine($"Attempt: {++current}");
InternalExecuteScript(path, processTimeOutMilliseconds, out _local_logs, out _local_success, args);
attempts_count--;
}
success = _local_success;
logs = _local_logs;
}
InternalExecuteScript 是 ingen 的代码
【问题讨论】:
-
实际进程在哪一行挂起?并更多地介绍您的代码
-
@Mr.AF 你是对的 - 完成了。
-
Powershell 的实际调用是一回事,但是您没有提供的是您在 Powershell 中尝试处理的脚本的实际其余部分。调用 powershell 本身不是问题,而是在您尝试做的事情之内。编辑您的帖子并放置您尝试执行的显式调用/命令。
-
我尝试复制错误真的很奇怪。它在 20 次尝试时随机发生了两次,我无法再次触发它。
-
@Joelty,哦,很有趣,你是说
Rx方法有效(因为它没有超时),即使流浪的 MSBuild 进程存在导致无限期等待?有兴趣了解如何处理
标签: c#