RedirectStandardOutput and the pause command in batch files
In my C# code, I have to run some external processes. These could be batch files or executables. I redirect the output to a window of my own by having RedirectStandardOutput
of the ProcessStartInfo
be true. This allows the user to review the output even after the external process has finished. I also redirect the input so the user can press keys if necessary. This works very well in general.
However, I have one problem. Some of the batch files use the PAUSE
command. Apparently the output isn't flushed by the command until a key has been pressed. Thus, the user never sees the prompt. So only after a key has been pressed will the user see that he should have pressed a key to continue. Is there a way to make this work (besides "Don't use PAUSE")?
EDIT: The batch files run the user's CAM post-processor and are usually created by the user (or the user's IT department). So requiring changes to these files or "hacking" by creating a pause.exe aren't really viable solutions (un开发者_StackOverflowless as very last resort).
In CMD, Try
batchWithPause.bat < nul
In C# that would be something like:
Process.Start("batchWithPause.bat", "< nul");
EDIT So you simply want to the user to see "Press any key to continue"? Then add
@echo "Press any key to continue..."
before each pause
call. You could also:
- Write your own pause.bat that does that essentially
Write a simple pause.exe, say in c#:
public static void Pause() { Console.WriteLine("Hit enter to continue."); Console.Read(); }
EDIT 2 I understand your problem now. Both the async OutputDataReceived and StreamReader.ReadLine() would read pause's "Press any key to continue" only after the user has pressed a key. However, if you were to loop StreamReader.Read() yourself, you would also get the "Press any key to continue". So this appears to be your only possible route.
精彩评论