简体   繁体   English

如何使命令提示符挂起?

[英]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.

相关问题 如何在“部署工具命令提示符”中运行批处理脚本(以制作WinPE iso)? - How can I run a batch script in 'Deployment Tools Command Prompt' (to make WinPE iso)? 如何从.bat文件在命令提示符下运行此命令? - How can I run this command in command prompt from a .bat file? 如何自动执行命令提示符的自定义过程? - How can I automate the process of customizing the command prompt? 如何在命令提示符下查看我的SYSTEM环境变量? - How can I view my SYSTEM environment variables in a command prompt? "如何在 Windows 批处理文件中提示“您确定”?" - How can I make an "are you sure" prompt in a Windows batchfile? 在Windows命令提示符下,如果没有输入,如何在一定时间内运行一个命令,如果没有输入,又如何运行? - In Windows Command prompt, how can I run one command if there is no input, and another if there is, within a certain time? 如何创建 .bat 文件以使用 VS2012 开发人员命令提示符运行“devenv”命令? - How can i create .bat file to run "devenv" command with VS2012 Developer Command Prompt? 如何在命令提示符 window 中使用 powershell 多行命令作为单行 - How can I use a powershell multi-line command as a single line in a command prompt window 如何使用for命令从文本文件捕获数据并将其设置为变量(Windows命令提示符) - How can i capture data using the for command from a text file and set it as a variable (windows command prompt) 如何使在命令提示符 window 中运行的批处理代码无法关闭? - How would I make batch code running in a command prompt window unclosable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM