종료를 기다리는 동안 프로세스가 중단 된 이유는 무엇입니까?
이 코드는 내부에서 많은 작업을 수행하는 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 빌드를 실행하기 위해 다시 시도하는 작은 래퍼를 추가했습니다.
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;
}
InternalExecuteScriptIngen의 코드는 어디에 있습니까
Rx은 길을 잃은 MSBuild 프로세스가 무한 대기로 이어지는 경우에도 접근 방식이 시간 초과되지 않은 것처럼 작동 한다고 말하고 있습니까? 그것이 어떻게 처리되었는지 알고 싶어