Browse Source

Merge branch 'main' of https://github.com/ionite34/StabilityMatrix into on-exit

pull/5/head
JT 2 years ago
parent
commit
192d157999
  1. 15
      StabilityMatrix/Helper/ProcessRunner.cs
  2. 16
      StabilityMatrix/PyVenvRunner.cs

15
StabilityMatrix/Helper/ProcessRunner.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
@ -28,7 +29,11 @@ public static class ProcessRunner
return output;
}
public static Process StartProcess(string fileName, string arguments, Action<string?>? outputDataReceived = null)
public static Process StartProcess(
string fileName,
string arguments,
Action<string?>? outputDataReceived = null,
Dictionary<string, string>? environmentVariables = null)
{
Logger.Trace($"Starting process '{fileName}' with arguments '{arguments}'");
var process = new Process();
@ -38,6 +43,14 @@ public static class ProcessRunner
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
if (environmentVariables != null)
{
foreach (var (key, value) in environmentVariables)
{
process.StartInfo.EnvironmentVariables[key] = value;
}
}
if (outputDataReceived != null)
{

16
StabilityMatrix/PyVenvRunner.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using StabilityMatrix.Helper;
@ -64,14 +65,25 @@ public class PyVenvRunner: IDisposable
}
}
public void RunDetached(string arguments, Action<string?> outputDataReceived, Action<int>? onExit = null)
public void RunDetached(string arguments, Action<string?> outputDataReceived, Action<int>? onExit = null, bool unbuffered = true)
{
if (!Exists())
{
throw new InvalidOperationException("Venv python process does not exist");
}
Debug.WriteLine($"Launching RunDetached at {PythonPath} with args {arguments}");
Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived);
if (unbuffered)
{
var env = new Dictionary<string, string>
{
{"PYTHONUNBUFFERED", "1"}
};
Process = ProcessRunner.StartProcess(PythonPath, "-u " + arguments, outputDataReceived, env);
}
else
{
Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived);
}
if (onExit != null)
{
Process.Exited += (_, _) => onExit(Process.ExitCode);

Loading…
Cancel
Save