I need my C# program to interact with the stdOut and stdErr of a DOS console application. Reading its text output asynchronously and poking text back into it to answer its prompts.

It works, but I'm having a problem with it on one machine. Not certain why yet because I can't debug the machine that is having the problem. But I have a suspicion I'm wondering about. It worked fine until they fixed a problem with the machine being too slow. Now that the machine is fast enough, it fails.

I think it might be missing the very tip, the very start of the very first prompt of the app's output. Not certain, but I think that might be it.

Pseudo-code of what my program is doing:
Code:
           // Create a process object which will allow us to run the process and control it
            this.process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = exeFileName,
                    WindowStyle = ProcessWindowStyle.Normal,
                    RedirectStandardOutput = true, // So that we can redirect and interpret the console output
                    RedirectStandardInput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    ErrorDialog = false,
                }
            };

            // Special event handlers to allow the system to gather the text output from the running exe so that we can check its contents
            this.process.OutputDataReceived += new DataReceivedEventHandler(ConsoleOutputReceived);
            this.process.ErrorDataReceived += new DataReceivedEventHandler(ErrorOutputReceived);

            // Initialize the value of the text string that will be updated by the event handlers
            this.OutputText = string.Empty;

            // Launch the process and start its output event handlers
            this.process.Start();
			
			// ***************************** NOTE: CANNOT PLACE THESE NEXT TWO LINES BEFORE PROCESS.START.
			// ***************************** Runtime won't let you. Gets a runtime error that the process
			// ***************************** isn't started yet.
			// ***************************** I wonder if this is the problem: the process starts too quickly
			// ***************************** and the first burst of output is lost and never caught by
			// ***************************** these event handlers on that system?
            this.process.BeginOutputReadLine();
            this.process.BeginErrorReadLine();

			
			// ********** Then there is a loop here which waits for certain strings to be received and writes to the standard input
			// ********** The problem occurs because, on one system, no text ever appears. Well actually one blank line appears.
			// ********** Which is interesting because a blank line is the last line of the output from the program before it waits
			// ********** for user input.
			

      	// *********************************************************************
        // Farther down, these are the separate functions to handle the outputs:
        // *********************************************************************
			
        private void ConsoleOutputReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("StdOut: " + e.Data.SafeToStringNeutral());    // So I can see what's happening
            this.OutputText += e.Data.SafeToStringNeutral();
        }

        private void ErrorOutputReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("StdErr: " + e.Data.SafeToStringNeutral());    // So I can see what's happening
            this.OutputText += e.Data.SafeToStringNeutral();
        }



What I can't figure out is, in an architecture like the one above, how do I capture the output of those first few characters, the ones before it can get its redirection handlers running?

All the other options I'm seeing use process.StandardOutput.ReadToEnd() or process.StandardOutput.ReadLine(), but those sit there and hang until the process exits, and I can't have that because I need to interact with it.

I've searched the internet, but they always show variants of the Synchronous and Asynchronous examples cited here http://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output ... I need a hybrid approach where I synchronously read the beginning lines without hanging, and then switch to asynchronous after those are all in.

Any ideas?
_________________________
Tony Fabris