3

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.

Community
  • 1
  • 1
Codana
  • 96
  • 1
  • 7
  • "redirect the output to the second textbox", why not using listbox instead of textbox? – David May 27 '13 at 03:38
  • I am kind of new to working with WinForms, so might I ask the benefit of using listbox over textbox? Is there something with that which would help with my input issue? – Codana May 27 '13 at 04:01
  • Using ReadToEnd() is the problem. The End is when the program exits. Which probably never happens because you can't give input to the program. So it just deadlocks. You'll need to use the Process.BeginOutput/ErrorReadLine() methods instead. – Hans Passant May 27 '13 at 09:19
  • Ah, okay. I just tried changing the last three lines to: `writer.WriteLine(thirdBox.Text); secondBox.Text += reader.ReadLine();` Then with the program from the first textbox, I did `System.Console.WriteLine( System.Console.ReadLine() );` with something entered in that third textbox beforehand. It correctly took in from the third textbox into the second one, like I wanted! Thanks for pointing out that how I had done the outputs was interfering with inputting. Given those suggested methods, it looks like I need to look more into asynchronous programming to get it to be interactive? – Codana May 27 '13 at 17:27

0 Answers0