如何使命令提示符挂起?

时间:2022-03-07 00:23:28

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?

1 个解决方案

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

这适合我。它将 重定向到self以防止ctrl>

@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“\ ”> NUL行查找“hang”的脚本参数。如果找到,脚本将执行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.

通常,可以使用Ctrl + C打破waitfor。但由于Ctrl + C的常规行为被start / b和 击败,除非用户执行ctrl>

The red X still works, though.

然而,红色X仍然有效。

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

这适合我。它将 重定向到self以防止ctrl>

@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“\ ”> NUL行查找“hang”的脚本参数。如果找到,脚本将执行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.

通常,可以使用Ctrl + C打破waitfor。但由于Ctrl + C的常规行为被start / b和 击败,除非用户执行ctrl>

The red X still works, though.

然而,红色X仍然有效。