2
votes

Hi there and thanking in advance

I am trying (very hard) to redirect Console input and output into a textbox. So far output is working fine but the trouble is with input. For example I cannot execute a simple program that will do the following:

Console.WriteLine("Please enter your name: "); string name = Console.ReadLine(); Console.WriteLine("Hi there " + name);

The reason I can't achieve this is because that the program has to stop while waiting for user to type his/her name and press enter. If I wait for user input on a new thread then the main GUI thread freezes and the textbox can never receive the KeyPress. This thing has me totally stumped. Any advice (or better still code) would be greatly appreciated.

Cheers

1
Possible dupe: stackoverflow.com/questions/415620/…Aryabhatta
Is the input happening on the GUI side, or the Console side? That is, do you have a TextBox control (for example) which, when text is entered, sends the text to the Console process (Process.StandardInput.WriteLine())?TreDubZedd

1 Answers

0
votes

The code below is a Console app that calls another console app to do some work and not a WinForm app, but you could easily replace the event handlers (TransformProcessOutputDataReceived, and TransformProcessErrorDataReceived) to output to a text box instead of a TextWriter. Unfortunately it doesn't directly address your issue of the called console application waiting for user input. The code below does pump some input to the called console app via standard input, so perhaps you could supply it from your windows app in the same manner.

Hope this was helpful, I had a heck of a time getting it to work originally myself, sorry I forgot the original references I had used, it was a while ago.

private static void ProcessRetrievedFiles(List<string> retrievedFiles)
    {
        Console.WriteLine();
        Console.WriteLine("Processing retrieved files:");
        Console.WriteLine("---------------------------");
        Console.WriteLine();
        foreach (string filePath in retrievedFiles)
        {
            if (String.IsNullOrEmpty(filePath)) continue;

            Console.WriteLine(filePath);
            Process transformProcess = new Process();

            string baseOutputFilePath = Path.Combine(ExportDirectory, Path.GetFileNameWithoutExtension(filePath));

            transformProcess.StartInfo.FileName = TransformerExecutablePath;
            transformProcess.StartInfo.Arguments = string.Format(
                "-i:\"{0}\" -x:\"{1}\" -o:\"{2}.final.xml\"",
                filePath,
                string.Empty,
                baseOutputFilePath);
            transformProcess.StartInfo.UseShellExecute = false;
            transformProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            transformProcess.StartInfo.RedirectStandardError = true;
            transformProcess.StartInfo.RedirectStandardOutput = true;
            transformProcess.StartInfo.RedirectStandardInput = true;

            transformProcess.EnableRaisingEvents = true;

            //attach the error/output recievers for logging purposes
            transformProcess.ErrorDataReceived += TransformProcessErrorDataReceived;
            transformProcess.OutputDataReceived += TransformProcessOutputDataReceived;

            ProcessBridgeFileOutputWriter = new StreamWriter(
                    baseOutputFilePath + ".log",
                    false);

            ProcessBridgeFileOutputWriter.AutoFlush = true;

            transformProcess.Start();

            transformProcess.BeginOutputReadLine();
            transformProcess.BeginErrorReadLine();

            //the exe asks the user to press a key when they are done...
            transformProcess.StandardInput.Write(Environment.NewLine);
            transformProcess.StandardInput.Flush();

            //because we are not doing this asynchronously due to output writer
            //complexities we don't want to deal with at this point, we need to 
            //wait for the process to complete
            transformProcess.WaitForExit();

            ProcessBridgeFileOutputWriter.Close();
            ProcessBridgeFileOutputWriter.Dispose();

            //detach the error/output recievers
            transformProcess.ErrorDataReceived -= TransformProcessErrorDataReceived;
            transformProcess.OutputDataReceived -= TransformProcessOutputDataReceived;
        }
    }

    static void TransformProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Data))
        {
            ProcessBridgeFileOutputWriter.WriteLine(e.Data);
        }
    }

    static void TransformProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Data))
        {
            ProcessBridgeFileOutputWriter.WriteLine(string.Format("ERROR: {0}", e.Data));
        }
    }