Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Refactor core process extensions #789

Merged
merged 8 commits into from
Nov 18, 2020
38 changes: 18 additions & 20 deletions src/Microsoft.Tye.Core/ProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void KillTree(this System.Diagnostics.Process process, TimeSpan ti
"taskkill",
$"/T /F /PID {pid}",
timeout,
out var _);
out _);
}
else
{
Expand All @@ -51,26 +51,24 @@ private static void GetAllChildIdsUnix(int parentId, ISet<int> children, TimeSpa
timeout,
out var stdout);

if (!string.IsNullOrEmpty(stdout))
if (string.IsNullOrEmpty(stdout))
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.


using var reader = new StringReader(stdout);
while (true)
{
using (var reader = new StringReader(stdout))
var text = reader.ReadLine();
if (text == null)
{
while (true)
{
var text = reader.ReadLine();
if (text == null)
{
return;
}

if (int.TryParse(text, out var id))
{
children.Add(id);
// Recursively get the children
GetAllChildIdsUnix(id, children, timeout);
}
}
return;
}

if (!int.TryParse(text, out var id))
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: prefer braces around ifs.


children.Add(id);
// Recursively get the children
GetAllChildIdsUnix(id, children, timeout);
}
}
catch (Win32Exception ex) when (ex.Message.Contains("No such file or directory"))
Expand Down Expand Up @@ -106,12 +104,12 @@ private static void RunProcessAndWaitForExit(string fileName, string arguments,
UseShellExecute = false,
};

var process = System.Diagnostics.Process.Start(startInfo);
var process = Process.Start(startInfo);

stdout = null;
if (process?.WaitForExit((int)timeout.TotalMilliseconds) == true)
{
stdout = process?.StandardOutput.ReadToEnd();
stdout = process.StandardOutput.ReadToEnd();
}
else
{
Expand Down