implement async read for net6 process
This commit is contained in:
parent
44010e91df
commit
5e652bcd59
@ -1,4 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using SPTInstaller.Models;
|
||||
|
||||
namespace SPTInstaller.Helpers;
|
||||
@ -57,4 +59,60 @@ public static class ProcessHelper
|
||||
return Result.FromError("an unknown error occurred in the patcher");
|
||||
}
|
||||
}
|
||||
|
||||
public static ReadProcessResult RunAndReadProcessOutputs(string fileName, string args, int timeout = 5000)
|
||||
{
|
||||
using var proc = new Process();
|
||||
|
||||
proc.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = fileName,
|
||||
Arguments = args,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
var outputBuilder = new StringBuilder();
|
||||
var errorBuilder = new StringBuilder();
|
||||
|
||||
using AutoResetEvent outputWaitHandle = new AutoResetEvent(false);
|
||||
using AutoResetEvent errorWaitHandle = new AutoResetEvent(false);
|
||||
|
||||
proc.OutputDataReceived += (s, e) =>
|
||||
{
|
||||
if (e.Data == null)
|
||||
{
|
||||
outputWaitHandle.Set();
|
||||
}
|
||||
else
|
||||
{
|
||||
outputBuilder.AppendLine(e.Data);
|
||||
}
|
||||
};
|
||||
|
||||
proc.ErrorDataReceived += (s, e) =>
|
||||
{
|
||||
if (e.Data == null)
|
||||
{
|
||||
errorWaitHandle.Set();
|
||||
}
|
||||
else
|
||||
{
|
||||
errorBuilder.AppendLine(e.Data);
|
||||
}
|
||||
};
|
||||
|
||||
proc.Start();
|
||||
|
||||
proc.BeginOutputReadLine();
|
||||
proc.BeginErrorReadLine();
|
||||
|
||||
if (!proc.WaitForExit(timeout) || !outputWaitHandle.WaitOne(timeout) || !errorWaitHandle.WaitOne(timeout))
|
||||
{
|
||||
return ReadProcessResult.FromError("Process timed out");
|
||||
}
|
||||
|
||||
return ReadProcessResult.FromSuccess(outputBuilder.ToString(), errorBuilder.ToString());
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ using SPTInstaller.Models;
|
||||
using System.Diagnostics;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using SPTInstaller.Helpers;
|
||||
|
||||
namespace SPTInstaller.Installer_Tasks.PreChecks;
|
||||
|
||||
@ -32,17 +33,14 @@ public class NetCore6PreCheck : PreCheckBase
|
||||
|
||||
try
|
||||
{
|
||||
var proc = Process.Start(new ProcessStartInfo()
|
||||
var result = ProcessHelper.RunAndReadProcessOutputs("dotnet", "--list-runtimes");
|
||||
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
FileName = "dotnet",
|
||||
Arguments = "--list-runtimes",
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
});
|
||||
return PreCheckResult.FromError(result.Message);
|
||||
}
|
||||
|
||||
proc.WaitForExit();
|
||||
|
||||
output = proc.StandardOutput.ReadToEnd().Split("\r\n");
|
||||
output = result.StdOut.Split("\r\n");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
18
SPTInstaller/Models/ReadProcessResult.cs
Normal file
18
SPTInstaller/Models/ReadProcessResult.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace SPTInstaller.Models;
|
||||
|
||||
public class ReadProcessResult : Result
|
||||
{
|
||||
public string StdOut { get; }
|
||||
public string StdErr { get; }
|
||||
|
||||
protected ReadProcessResult(string message, bool succeeded, string stdOut = "", string stdErr = "") : base(message, succeeded)
|
||||
{
|
||||
StdOut = stdOut;
|
||||
StdErr = stdErr;
|
||||
}
|
||||
|
||||
public static ReadProcessResult FromSuccess(string stdOut, string stdErr) =>
|
||||
new ReadProcessResult("ok", true, stdOut, stdErr);
|
||||
|
||||
public new static ReadProcessResult FromError(string message) => new ReadProcessResult(message, false);
|
||||
}
|
@ -9,8 +9,8 @@
|
||||
<PackageIcon>icon.ico</PackageIcon>
|
||||
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
|
||||
<Configurations>Debug;Release;TEST</Configurations>
|
||||
<AssemblyVersion>2.22.1</AssemblyVersion>
|
||||
<FileVersion>2.22.1</FileVersion>
|
||||
<AssemblyVersion>2.23</AssemblyVersion>
|
||||
<FileVersion>2.23</FileVersion>
|
||||
<Company>SPT-AKI</Company>
|
||||
</PropertyGroup>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user