อะไรเป็นสาเหตุที่ทำให้กระบวนการของฉันหยุดชะงักในระหว่างรอทางออก
รหัสนี้จะต้องเริ่มสคริปต์ 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 ฉันได้เพิ่ม wrapper ขนาดเล็กซึ่งพยายามที่จะดำเนินการแขวน 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รหัสของไอเอ็นจีอยู่ที่ไหน
Rxวิธีการทำงาน (ในขณะที่มันไม่ได้หมดเวลา) แม้จะมีกระบวนการ MSBuild หลงทางอยู่รอบ ๆ นำไปสู่การรออย่างไม่มีกำหนด? สนใจที่จะทราบวิธีการจัดการ