mirror of
https://github.com/Wessel/Roommapper.git
synced 2026-07-20 06:53:58 +02:00
50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
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();
|
|
|
|
}
|
|
}
|