RedirectStandardOutput和批处理文件中的pause命令 [英] RedirectStandardOutput and the pause command in batch files

查看:59
本文介绍了RedirectStandardOutput和批处理文件中的pause命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#代码中,我必须运行一些外部进程.这些可能是批处理文件或可执行文件.通过将 ProcessStartInfo RedirectStandardOutput 设置为true,可以将输出重定向到我自己的窗口.这样,即使在外部过程完成后,用户也可以查看输出.我还重定向输入,以便用户在必要时可以按键.一般而言,这很好.

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.

但是,我有一个问题.一些批处理文件使用 PAUSE 命令.显然,直到按下键之前,命令不会刷新输出.因此,用户永远不会看到提示.因此,只有在按下某个键之后,用户才会看到他应该已经按下某个键才能继续.有没有办法做到这一点(除了不要使用暂停")?

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")?

批处理文件运行用户的CAM后处理器,通常由用户(或用户的IT部门)创建.因此,要求更改这些文件或通过创建pause.exe进行黑客入侵"并不是真正可行的解决方案(除非万不得已).

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).

推荐答案

在CMD中,尝试

batchWithPause.bat < nul

在C#中,类似于:

 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. 编写自己的pause.bat,基本上可以做到
  2. 编写一个简单的pause.exe,用c#表示:

  1. Write your own pause.bat that does that essentially
  2. Write a simple pause.exe, say in c#:

公共静态无效Pause(){Console.WriteLine(点击进入以继续.");Console.Read();}

public static void Pause() { Console.WriteLine("Hit enter to continue."); Console.Read(); }

编辑2 ,我现在了解您的问题.仅在用户按下一个键之后,异步OutputDataReceived和StreamReader.ReadLine()都将读取暂停的按任意键继续".但是,如果您要自己循环StreamReader.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.

这篇关于RedirectStandardOutput和批处理文件中的pause命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆