I am working on a sort of IDE-like program in WinForms in Visual Studio 2010 with C#. I've gotten it to work with two textboxes, two buttons, and a console window. The first textbox is used for writing C# code, which is compiled using CodeDomProvider
by hitting a compile button. Then any issues or a message of success appears in the second textbox. If it's a success, you can hit the run button for it to run in a console window.
What I would like to do is not have the compiled program run in a separate window.
I've managed to figure out how to redirect the output to the second textbox, but I am having difficulty getting any input. I even made a third textbox with text put in beforehand to try to just get some sort of input in, but the form ends up freezing. You can see this in the code below.
private void runButton_Click(object sender, EventArgs e)
{
secondBox.Text = "";
Process process = new Process();
process.StartInfo.FileName = Program;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.CreateNoWindow = true;
process.Start();
StreamReader reader = process.StandardOutput;
StreamReader eReader = process.StandardError;
StreamWriter writer = process.StandardInput;
secondBox.Text += reader.ReadToEnd();
secondBox.Text += eReader.ReadToEnd();
writer.WriteLine(thirdBox.Text);
}
(I know just adding in input and output one time isn't going to cut it for what I want to do, but I'm taking it one step at a time.)
I tried doing like this example: Redirect console input and output to a textbox.
I nixed the writer
bits, going with process.StandardInput.WriteLine(thirdBox.Text);
instead, but that still didn't work.
There is also a problem of errors being generated when trying to use something like System.Console.ReadKey()
within the compiled program because the real process window is not actually created (process.StartInfo.CreateNoWindow = true;
). I found this on the subject: C#: can't ReadKey if my parent is a window application and it is redirecting my stdout, but I am not sure what to understand from that other than possibly running it in another console window?
I've found a lot of information on transferring console output (which helped me figure that out) but not so much with input. Some pointing in the right direction would be appreciated, either with the input from textbox or with another way to embed the compiled program. Thank you.