fix: Meer fixes

This commit is contained in:
2024-07-02 23:44:22 +02:00
parent 045f8f1d22
commit ec4f9b4a0e

View File

@@ -0,0 +1,49 @@
using System.Diagnostics;
using System.Text;
namespace MotorController;
class ChildProcess {
StringBuilder outputBuilder = new();
ProcessStartInfo processStartInfo;
Process process;
void Spawn() {
// Spawn a new ChildProcess
processStartInfo = new ProcessStartInfo {
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
Arguments = "./stepper.py drive",
FileName = "/usr/bin/python3"
};
process = new Process {
StartInfo = processStartInfo,
// enable raising events because Process does not raise events by default
EnableRaisingEvents = true
};
// attach the event handler for OutputDataReceived before starting the process
process.OutputDataReceived += new DataReceivedEventHandler(
delegate (object sender, DataReceivedEventArgs e) {
// append the new data to the data already read-in
outputBuilder.Append(e.Data);
}
);
// start the process
// then begin asynchronously reading the output
// then wait for the process to exit
// then cancel asynchronously reading the output
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();
// use the output
string output = outputBuilder.ToString();
}
}