RedirectStandardIn Stops Output - c#

I have a GUI that wraps a console application to improve user experience. I'm using the Process class to spin off the console executable. I want the console window to come up, and I will manually write abbreviated output to it. Then, once the executable is finished, the console window will close and control passes back to the GUI.
However, it is an interactive program, requiring me to redirect both StandardIn and StandardOut. The problem is that the very act of redirecting the input stops all output from coming to the console.
I've included all the output code as I had it, but commented out. As it is, a console window will open and the prompts will appear, waiting for user input. If I uncomment RedirectStandardIn, the window appears, but simply remains blank. Am I misunderstanding the role of Process.StandardInput?
class HandleExecutable
{
...
public void callExecutable(string executable, string args, string inputStr)
{
string commandLine = executable;
ProcessStartInfo psi = new ProcessStartInfo(commandLine);
psi.UseShellExecute = false;
psi.LoadUserProfile = false;
//psi.RedirectStandardOutput = true;
//psi.RedirectStandardError = true;
/* UNCOMMENT BELOW WILL CAUSE NO OUTPUT TO BE PUT TO THE CONSOLE */
//psi.RedirectStandardInput = true;
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.Arguments = args;
Process p = new Process();
p.StartInfo = psi;
try
{
p.Start();
//p.StandardInput.WriteLine(inputStr);
//p.BeginOutputReadLine();
//p.BeginErrorReadLine();
if (outputHandler != null) p.OutputDataReceived += outputHandler;
if (errorHandler != null) p.ErrorDataReceived += errorHandler;
p.WaitForExit();
p.Close();
p.Dispose();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
}

Related

C# Redirect other console app StandardOutput fail if I won't close StardandInput

I've got a tricky issue with a console app, from which I'm trying to redirect StandardInput, StandardOutput and StandardError.
I've got a working solution for other console app - that's not a new thing for me. But this app seems to have something special, which is making impossible to redirect all outputs with a standard approach.
Console app works like this:
directly after startup writes a few lines and waits for user input
no matter what input is - console app is showing some output and again wait for new input
console app never ends, it has to be closed
I've tried already solutions based on:
StandardOutput/Error.ReadToEnd()
taki care of OutputDataReceived & ErrorDataReceived with read line by line with ReadLine
reading by char
waiting for the end of process (which is not ending, so I'm running into a deadlock)
to start console app in a preloaded cmd.exe and grab this (StandardOutput stopped to show just after launch of this console app)
to manually flush input
All the time I had completely no output and no error stream from console app - nothing.
After a multitude attempts I've discovered, that I can receive StandardOutput only when I'll close StandardInput after programatically inputting the data.
But in this case, console app is going wild, falling into loop of writing few lines to StandardOutput as on start-up, which makes final output big and full of garbages.
With MedallionShell library I'm able to try to gracefully kill it with Ctrl+C, but this solution is still far from acceptable, because:
sometimes console app will produce so much garbages before I will be able to kill it, that it crashes my app
even if this won't crash, searching for expected output in a lot of garbages is nasty and tragically slows down automatization (6000 records in... 15 minutes)
I'm unable to provide more than one input at a time, so I have to start console app just to receive one output, close and start again for another output
I've got no sources for that console app, so I'm even not able to recreate the issue or fix it - it's some very legacy app at my company, which I'm trying to make at least a bit automatic...
Code, with which I've got at least anything now (with MediallionShell):
var command = Command.Run(Environment.CurrentDirectory + #"\console_app.exe");
command.StandardInput.WriteLine("expected_input");
command.StandardInput.Close(); // without that I'll never receive any output or error stream from this stubborn console app
command.TrySignalAsync(CommandSignal.ControlC); // attempt to kill gracefully, not always successfull...
var result = command.Result;
textBox1.AppendText(String.Join(Environment.NewLine, command.GetOutputAndErrorLines().ToArray().Take(10))); // trying to get rid of garbages
command.Kill(); // additional kill attempt if Ctrl+C didn't help, sometimes even this fails
Any help will be appreciated, I'm also still searching for solution and now I'm checking this one: PostMessage not working with console window whose output is redirected and read asynchronously but author there had an output and I don't...
You haven't provided a sample Console program to test with, but something like the following may work:
Create a Console project (Console (.NET Framework)) - used for testing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleTestApp
{
class Program
{
static void Main(string[] args)
{
//prompt for input - 1st prompt
Console.Write("Please enter filename: ");
string filename = Console.ReadLine();
if (System.IO.File.Exists(filename))
{
Console.WriteLine("'" + filename + "' exists.");
}
else
{
Console.Error.WriteLine("'" + filename + "' doesn't exist.");
}
//prompt for input - 2nd prompt
Console.Write("Would you like to exit? ");
string answer = Console.ReadLine();
Console.WriteLine("Your answer was: " + answer);
Console.WriteLine("Operation complete.");
}
}
}
Then, create a Windows Forms project Windows Forms (.NET Framework) and run one of the following:
Option 1:
private void RunCmd(string exePath, string arguments = null)
{
//create new instance
ProcessStartInfo startInfo = new ProcessStartInfo(exePath, arguments);
startInfo.Arguments = arguments; //arguments
startInfo.CreateNoWindow = true; //don't create a window
startInfo.RedirectStandardError = true; //redirect standard error
startInfo.RedirectStandardOutput = true; //redirect standard output
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false; //if true, uses 'ShellExecute'; if false, uses 'CreateProcess'
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.ErrorDialog = false;
//create new instance
using (Process p = new Process { StartInfo = startInfo, EnableRaisingEvents = true })
{
//subscribe to event and add event handler code
p.ErrorDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Debug.WriteLine("Error: " + e.Data);
}
};
//subscribe to event and add event handler code
p.OutputDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Debug.WriteLine("Output: " + e.Data);
}
};
p.Start(); //start
p.BeginErrorReadLine(); //begin async reading for standard error
p.BeginOutputReadLine(); //begin async reading for standard output
using (StreamWriter sw = p.StandardInput)
{
//provide values for each input prompt
//ToDo: add values for each input prompt - changing the for loop as necessary
for (int i = 0; i < 2; i++)
{
if (i == 0)
sw.WriteLine(#"C:\Temp\Test1.txt"); //1st prompt
else if (i == 1)
sw.WriteLine("Yes"); //2nd prompt
else
break; //exit
}
}
//waits until the process is finished before continuing
p.WaitForExit();
}
}
Option 2:
private void RunCmd(string exePath, string arguments = null)
{
//create new instance
ProcessStartInfo startInfo = new ProcessStartInfo(exePath, arguments);
startInfo.Arguments = arguments; //arguments
startInfo.CreateNoWindow = true; //don't create a window
startInfo.RedirectStandardError = true; //redirect standard error
startInfo.RedirectStandardOutput = true; //redirect standard output
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false; //if true, uses 'ShellExecute'; if false, uses 'CreateProcess'
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.ErrorDialog = false;
//create new instance
using (Process p = new Process { StartInfo = startInfo, EnableRaisingEvents = true })
{
//subscribe to event and add event handler code
p.ErrorDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Debug.WriteLine("Error: " + e.Data);
}
};
//subscribe to event and add event handler code
p.OutputDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Debug.WriteLine("Output: " + e.Data);
}
};
p.Start(); //start
p.BeginErrorReadLine(); //begin async reading for standard error
p.BeginOutputReadLine(); //begin async reading for standard output
using (StreamWriter sw = p.StandardInput)
{
//provide values for each input prompt
//ToDo: add values for each input prompt - changing the for loop as necessary
sw.WriteLine(#"C:\Temp\Test1.txt"); //1st prompt
sw.WriteLine("Yes"); //2nd prompt
}
//waits until the process is finished before continuing
p.WaitForExit();
}
}
Option 3:
Note: This one is modified from here.
private void RunCmd(string exePath, string arguments = null)
{
//create new instance
ProcessStartInfo startInfo = new ProcessStartInfo(exePath, arguments);
startInfo.Arguments = arguments; //arguments
startInfo.CreateNoWindow = true; //don't create a window
startInfo.RedirectStandardError = true; //redirect standard error
startInfo.RedirectStandardOutput = true; //redirect standard output
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false; //if true, uses 'ShellExecute'; if false, uses 'CreateProcess'
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.ErrorDialog = false;
//create new instance
using (Process p = new Process { StartInfo = startInfo, EnableRaisingEvents = true })
{
p.Start(); //start
Read(p.StandardOutput);
Read(p.StandardError);
using (StreamWriter sw = p.StandardInput)
{
//provide values for each input prompt
//ToDo: add values for each input prompt - changing the for loop as necessary
sw.WriteLine(#"C:\Temp\Test1.txt"); //1st prompt
sw.WriteLine("Yes"); //2nd prompt
}
//waits until the process is finished before continuing
p.WaitForExit();
}
}
private static void Read(StreamReader reader)
{
new System.Threading.Thread(() =>
{
while (true)
{
int current;
while ((current = reader.Read()) >= 0)
Console.Write((char)current);
}
}).Start();
}

How to capture output of Python script in C# as it is printing text while running?

I have following Python script :
import time
for x in range(10):
print(x)
time.sleep(0.05)
I want to run this script and capture its output while it is running.
I used the following C# code but it does not print any number until it completely finishes loop.
private void DoScriptTest()
{
ProcessStartInfo start = new ProcessStartInfo();
string cmd = #"c:\flowers\count.py";
start.FileName = #"C:\Users\pubud\AppData\Local\Programs\Python\Python36\python.exe";
start.Arguments = string.Format("{0}", cmd);
start.UseShellExecute = false;// Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
Process process = new Process();
process.StartInfo = start;
process.EnableRaisingEvents = true;
process.OutputDataReceived += ProcessOutputHandler;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
}
private void ProcessOutputHandler(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data))
try {
TxtPrompt.Invoke((MethodInvoker)delegate {
TxtPrompt.AppendText(e.Data);
TxtPrompt.Refresh();
});
}
catch
{ }
}
I am not sure why ProcessOutputHandler is NOT called in while the script is running. How could I get that output ? (the numbers coming from Python script in real time)

Close Console Window Upon Starting A New Build In Main

In main of my console application I am running an elevated version of my application if the user requests to run it elevated.
the code is doing this
elevated.exe myexe.exe //a /elevated
This code is being run in main so what happens when myexe is ran it opens a console window hits the code below and creates another console window with the new instance.
How do I close the initial window programmatically without closing the new one?
Environment.Exit(0) //closes the entire application THIS WONT WORK
enter code here
public void Run(string[] args) //myexe.exe
{
if (args[0] == "/elevated")
{
_command.RunElevated(path, arguments);
return;
}
}
Here is the meat of the RunElevated code pretty standard..
var process = new Process();
if (workingDirectory != null) process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = fileName;
process.StartInfo.Arguments = arguments;
process.Start();
// deal with output
standardOutput = process.StandardOutput.ReadToEnd();
standardError = process.StandardError.ReadToEnd();
// wait til the process exits
process.WaitForExit();
int exitCode = process.ExitCode;
OK maybe I know what's going on now. When you use UseShellExecute = false, then the program runs in the same command window, which you are closing with Environment.Exit(0).
So change to this:
var process = new Process();
if (workingDirectory != null) process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = fileName;
process.StartInfo.Arguments = arguments;
process.Start();
Do no redirect output because 1) you can't with UseShellExecute=true, and 2) you are closing your main application anyway so why redirect things to an app that is exiting in a couple milliseconds.
With these changes you spawn your app in its own, hidden, window, then just Environment.Exit(0) your main application which will kill the non-elevated one but won't touch the process you spawned.
Here's an entirely working example:
using System;
using System.Diagnostics;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "/elevated")
{
var process = new Process();
/*process.StartInfo.WorkingDirectory = */
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = "ConsoleApplication4.exe";
process.StartInfo.Arguments = "startedThroughElevatedCodePath";
process.Start();
Environment.Exit(0);
}
if (args.Length > 0 && args[0] == "startedThroughElevatedCodePath")
{
Console.WriteLine("Hello from elevated");
}
else
{
Console.WriteLine("Hello from not elevated");
}
Console.Read();
}
}
}

Fail to open file in C# after editing it in another process

I'm using a helper class for running external process:
class ExternalProcessRunner
{
static public string Run(string program, string parameters)
{
output = "";
error = "";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = program;
startInfo.Arguments = parameters;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
StringBuilder outputSB = new StringBuilder();
StringBuilder errorSB = new StringBuilder();
using (Process exeProcess = Process.Start(startInfo))
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
exeProcess.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
outputSB.AppendLine(e.Data);
}
};
exeProcess.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
errorSB.AppendLine(e.Data);
}
};
exeProcess.Start();
exeProcess.BeginOutputReadLine();
exeProcess.BeginErrorReadLine();
exeProcess.WaitForExit();
outputWaitHandle.WaitOne();
errorWaitHandle.WaitOne();
output = outputSB.ToString();
error = errorSB.ToString();
}
}
catch (Exception e)
{
return e.Message;
}
return "";
}
static public string output;
static public string error;
}
It is used to run a perl script which accepts a filename, opens a file, writes some information and closes a file. Then C# code opens that file for reading. Sometimes I get an exception:
"The process cannot access the file 'tmp_file.txt' because it is being used by another process."
What can cause the problem? How to fix it? I think that I'm ensuring the ending of process which means freeing all handles.
Please check you have exited the process i.e. you released the file from the memory.
You can pass in a set number of seconds to wait for the process to exit. Then check if it has exited. If it hasn't, then try and kill the process.
In the example below, it waits for 1 minute for the process to exit. If it doesn't exit, then it sensd a command to close the main window and sleep for 2 seconds. If it still hasn't exited then it tries to kill the process.
At the stage the external process should be gone and the lock released on the file.
exeProcess.WaitForExit(1 * 60 * 1000);
if (!exeProcess.HasExited)
{
_log.Warn("External process has not completed after {0} minutes - trying to close main window", waitTimeMin);
exeProcess.CloseMainWindow();
System.Threading.Thread.Sleep(2000);
}
if (!exeProcess.HasExited)
{
_log.Warn("External process still has not completed - Killing process and waiting for it to exit...");
exeProcess.Kill();
exeProcess.WaitForExit();
}

How to run a batch file within a C# GUI form

How would you execute a batch script within a GUI form in C#
Could anyone provide a sample please?
System.Diagnotics.Process.Start("yourbatch.bat"); ought to do it.
Another thread covering the same issue.
This example assumes a Windows Forms application with two text boxes (RunResults and Errors).
// Remember to also add a using System.Diagnostics at the top of the class
private void RunIt_Click(object sender, EventArgs e)
{
using (Process p = new Process())
{
p.StartInfo.WorkingDirectory = "<path to batch file folder>";
p.StartInfo.FileName = "<path to batch file itself>";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.WaitForExit();
// Capture output from batch file written to stdout and put in the
// RunResults textbox
string output = p.StandardOutput.ReadToEnd();
if (!String.IsNullOrEmpty(output) && output.Trim() != "")
{
this.RunResults.Text = output;
}
// Capture any errors written to stderr and put in the errors textbox.
string errors = p.StandardError.ReadToEnd();
if (!String.IsNullOrEmpty(errors) & errors.Trim() != ""))
{
this.Errors.Text = errors;
}
}
}
Updated:
The sample above is a button click event for a button called RunIt. There's a couple of text boxes on the form, RunResults and Errors where we write the results of stdout and stderr to.
I deduce that by executing within a GUI form you mean showing execution results within some UI-Control.
Maybe something like this:
private void runSyncAndGetResults_Click(object sender, System.EventArgs e)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"C:\batch.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process batchProcess;
batchProcess = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = batchProcess.StandardOutput;
batchProcess.WaitForExit(2000);
if (batchProcess.HasExited)
{
string output = myOutput.ReadToEnd();
// Print 'output' string to UI-control
}
}
Example taken from here.

Categories

Resources