是的,我知道您可能会抱怨说这是一件坏事,但是我还是想这样做!
我正在创建一个批处理程序,最后,我需要将其挂起并且不接受用户输入.我知道一种方法只是创建一个无限循环:
I am creating a batch program and at the end, I need it to hang and not accept user input. I know one method is just creating an infinite loop of:
:pause pause > nul goto pause但我认为这不是一个好选择.尽管我需要将其挂起,但是我需要能够通过窗口顶部的红色"X"关闭按钮将其关闭.
but I don't think that's a great choice. Although I need it to hang, I need to to be able to be closed via the red 'X' close button at the top of the window.
有什么想法吗?
推荐答案这对我有用.它将< NUL重定向到self中,以防止 Ctrl + C 损坏,并使用start /b /wait禁止显示终止批处理作业(Y/N)?".提示.
This works for me. It redirects < NUL into self to prevent Ctrl+C from breaking, and uses start /b /wait to suppress the "Terminate batch job (Y/N)?" prompts.
@echo off setlocal >NUL (echo(%* | findstr "\<hang\>" && waitfor redX) rem // *** PUT YOUR MAIN SCRIPT HERE *** echo End of line. rem // ******* END MAIN SCRIPT ********* call :hang goto :EOF :hang start /b /wait "" "%~f0" hang ^<NUL在脚本的首次启动时,echo(%* | findstr "\<hang\>" >NUL行查找脚本参数"hang".如果找到,该脚本将执行waitfor命令.
On the initial launch of the script, the echo(%* | findstr "\<hang\>" >NUL line looks for a script argument of "hang". If found, the script executes the waitfor command.
通常,可以使用 Ctrl + C 破坏waitfor.但是由于 Ctrl + C 的通常行为被start /b和<NUL击败,因此除非用户执行 Ctrl + 中断或发送应答waitfor信号.
Normally, waitfor can be broken with Ctrl+C. But since the usual behavior of Ctrl+C is defeated by start /b and <NUL, the hanging effect is achieved unless a user does Ctrl+Break or sends the answering waitfor signal.
尽管如此,红色的X仍然有效.
The red X still works, though.
更多推荐
如何使命令提示符挂起?
发布评论