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:
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…