views:

27

answers:

1

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 (unless as very last resort).

+1  A: 

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:

  1. Write your own pause.bat that does that essentially
  2. 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.

ohadsc
This redirects the input, so the pause command is skipped. But I want the pause command to work (and it does). The problem is that the text isn't written to the output, so the user does not know to press a key to continue.
Daniel Rose
@Daniel See my edit. Don't know why the markup is wrong in the pause() implementation
ohadsc
Unfortunately the batch files are supplied by the user (they run the CAM post-processor). Replacing the pause command (or telling them "Don't use pause") are two possible solution, but neither "solution" is really what I would want to do.
Daniel Rose
OK, see my edit
ohadsc
With those hints I got it working. However, two caveats: 1) `Read()` blocks if there is no char "waiting" in the output. So I run this on a separate thread. 2) Once we are done and no more characters are left, `EndOfStream` is true. Trying to read will result in an exception. However, checking this property also blocks if there is no char waiting on the output(as above).
Daniel Rose
Hmm, perhaps you could use ReadBlock? http://msdn.microsoft.com/en-us/library/system.io.textreader.readblock.aspx
ohadsc
Also, what you're saying is weird - Read is supposed to return -1 when it reaches the end of the stream.. perhaps you forgot to check for it ? http://msdn.microsoft.com/en-us/library/ath1fht8.aspx
ohadsc