1

I am writing a replacement Windows shell in C#, and the feature I am currently implementing is having all IO inside the same shell window - ie, not opening cmd.exe in another window.

Right now, I have a partial solution to output. This is my code (where p is the running Process):

while ( !p.HasExited ) {
    /* ... - irrelevant */
    if ( redirect ) {
        try {
            p.BeginOutputReadLine();
        } catch { }
    }
}

The process is set up with all the correct properties, such asUseShellExecute = False, RedirectStandard{Input, Output, Error} = True, and the event handlers are set up correctly, but it's inconsistent.

I have tried taking out the try/catch (which I know is extremely bad practice), and using a busy bool, which is set to false when the handler runs, but for some reason, I still get an InvalidOperationException on p.BeginOutputReadLine() - stating that there is already an async operation running.

Any help would be appreciated, even if it requires a completely different solution to the one above, rather than just fixing it.

Thanks.

EDIT: Here is the code which starts the process:

if (redirect)
{
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.UseShellExecute = false;
    p.OutputDataReceived += new DataReceivedEventHandler(redirectHandler_StdOut);
    p.ErrorDataReceived += new DataReceivedEventHandler(redirectHandler_StdErr);
}
p.Start();

Also, I've realised that I'm not explaining what I mean by inconsistent. As they say, a picture is worth 2^3 words:

screenshot

Lucas Jones
  • 19,767
  • 8
  • 75
  • 88

1 Answers1

3

Methods prefixed with Begin typically run asynchronously. Is this what you want? Because it appears that the loop runs as fast as it can, calling more and more BeginOutputReadLine (because the call returns immediately, before it's done and the loop comes around for another iteration).

You could call ReadLine on the StandardOutput stream for a synchronous solution. Or:

    while ( !p.HasExited ) {
        /* ... - irrelevant */
        if ( redirect && !busy ) {
            try {
                busy = true;
                p.BeginOutputReadLine();
            } catch { }
        }
    }
   ////In the method that gets called upon completion of BeginOutputReadLine
   busy = false;

Keep this in mind though (from MSDN):

When asynchronous read operations start, the event handler is called each time the associated Process writes a line of text to its StandardOutput stream.

You can cancel an asynchronous read operation by calling CancelOutputRead. The read operation can be canceled by the caller or by the event handler. After canceling, you can call BeginOutputReadLine again to resume asynchronous read operations.

That leads me to believe you should only call this method once and it will keep notifying you through callback when a line gets written.

colithium
  • 10,269
  • 5
  • 42
  • 57
  • I believe this is the issue as well; it just occurred to me, but you already wrote it for me :) – Fredrik Mörk Jun 29 '09 at 22:29
  • That's just about what I tried, except from the try..catch - I'll add that in. Is MSDN trying to say that you only need to call BeginOutputReadLine once? Or does it need to be resumed after every line? – Lucas Jones Jun 29 '09 at 22:30
  • (Not in this question, sorry!) I need to remember what's on the inbterwebs and what's not. – Lucas Jones Jun 29 '09 at 22:31
  • It sounds like it only needs to be called once with a delegate to your method that does something whenever a line is outputted. It will just keep calling that method after you set it up each time a new line is outputted. – colithium Jun 30 '09 at 07:25
  • OK, I'm back online now so I'll try that out. Thanks. – Lucas Jones Jun 30 '09 at 17:07