user4838695
user4838695

Reputation:

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.

Any ideas?

Upvotes: 0

Views: 1197

Answers (1)

rojo
rojo

Reputation: 24466

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.

Upvotes: 1

Related Questions