How Can I Make A Command Prompt Hang?

偶尔善良 提交于 2019-12-12 06:35:20

问题


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.

Any ideas?


回答1:


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

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.

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.

The red X still works, though.



来源:https://stackoverflow.com/questions/30223954/how-can-i-make-a-command-prompt-hang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!