Hi everyone,
I want to run a process in the windows console, after that, I want to pass (with button click) some commands and see the result in a RichTextBox.
I’m able to launch the program and read the responses after starting, but when I’m trying to send any commands, it doesn’t work. I’m not able to “speak” with the process…
So, if you have any ideas in order to write in the process, I will take it with pleasure !
I already tried :
and
.
Below the code :
I want to run a process in the windows console, after that, I want to pass (with button click) some commands and see the result in a RichTextBox.
I’m able to launch the program and read the responses after starting, but when I’m trying to send any commands, it doesn’t work. I’m not able to “speak” with the process…
So, if you have any ideas in order to write in the process, I will take it with pleasure !
I already tried :
C#:
Console.WriteLine
C#:
StandardInput
Below the code :
C#:
public static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data)) {
numOutputLines++;
// Add the text to the collected output.
sortOutput.Append(Environment.NewLine + $"[{numOutputLines}] - {outLine.Data}");
//RichTextBox
MCM.ActiveForm.Invoke(MCM.AffichageTextDelegate, new object[] { outLine.Data });
}
}
public static async Task<int> RunProcessAsync() {
using (var process = new Process {
StartInfo = {
FileName = "AAA.exe",
Arguments = “-v COM74",
UseShellExecute = false,
CreateNoWindow = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true
},
EnableRaisingEvents = true
})
{ return await RunProcessAsync(process).ConfigureAwait(false); }
}
private static Task<int> RunProcessAsync(Process process) {
var tcs = new TaskCompletionSource<int>();
process.Exited += (s, ea) => tcs.SetResult(process.ExitCode);
process.OutputDataReceived += (s, ea) => Console.WriteLine(ea.Data);
sortOutput = new StringBuilder();
process.OutputDataReceived += SortOutputHandler;
process.ErrorDataReceived += (s, ea) => Console.WriteLine("ERR: " + ea.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
return tcs.Task;
}
private async void Btn_Click(object sender, EventArgs e) {
await RunProcessAsync();