[英]How Can I Make A Command Prompt Hang?
Yes, I know you are probably going to complain saying it's a bad thing to do, but I want to do it anyway! 是的,我知道您可能会抱怨说这是一件坏事,但是我还是想这样做!
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
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. 尽管我需要将其挂起,但是我需要能够通过窗口顶部的红色“ X”关闭按钮将其关闭。
Any ideas? 有任何想法吗?
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)?" 它将< NUL
重定向到self以防止Ctrl + C中断,并使用start /b /wait
禁止显示“终止批处理作业(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
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. 在脚本的初始启动时, echo(%* | findstr "\\<hang\\>" >NUL
行将查找脚本参数“ hang”。如果找到,脚本将执行waitfor
命令。
Normally, waitfor
can be broken with Ctrl + C . 通常,可以使用Ctrl + C来中断waitfor
。 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. 但是由于Ctrl + C的常规行为被start /b
和<NUL
击败,所以除非用户执行Ctrl + Break或发送应答waitfor
信号,否则将实现挂起效果。
The red X still works, though. 红色X仍然可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.