【问题标题】:Combination of `sleep` and `pause` command`sleep` 和 `pause` 命令的组合
【发布时间】:2013-11-21 10:29:08
【问题描述】:

好的,在看到用这么少的代码完成疯狂的事情之后,我寄予厚望。

差不多,我想正常使用pause命令,但是,如果用户在指定的时间内没有输入任何内容,它会自动继续。

在伪代码中:

(sleep %sleep-time%&Echo Pass)1>0 & pause

起初我想我可以使用start /b 来创建一个在当前线程暂停时回显输入的进程,但如果用户确实输入了某些内容,这可能会导致问题。

奖金

如果错误级别会根据用户输入的内容或pause 命令超时而改变,那将是非常酷的。

【问题讨论】:

    标签: batch-file input sleep simultaneous


    【解决方案1】:

    我建议使用timeout:

    timeout /T 60 >NUL
    

    这将使您的脚本休眠 1 分钟,或者除非用户按下键。

    【讨论】:

    • 不需要这样做,但即使是 Monas 伪代码也可以稍作修改:(timeout /t 60 & echo d)>nul|pause
    【解决方案2】:
    @echo off
    setlocal
    
    rem TimedPause.bat - Antonio Perez Ayala
    
    if "%1" equ ":PausePart" goto PausePart
    if "%1" neq "" goto begin
    echo TimedPause.bat seconds
    echo/
    echo Wait for given seconds or until user press a key
    echo At end, the presence of keyPressed.txt file indicate the cause of exit
    goto :EOF
    
    :begin
    set seconds=%1
    start "" /B "%~F0" :PausePart
    for /F "skip=2 tokens=2 delims=," %%a in ('tasklist /FI "IMAGENAME eq cmd.exe" /FO CSV') do (
       set PausePart=%%a
       goto TimePart
    )
    
    :TimePart
       ping -n 2 localhost > NUL
       if exist keyPressed.txt goto :EOF
       set /A seconds-=1
    if %seconds% gtr 0 goto TimePart
    taskkill /PID %PausePart% /F > NUL
    goto :EOF
    
    :PausePart
    del keyPressed.txt 2> NUL
    pause
    echo %time% > keyPressed.txt
    exit
    

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      相关资源
      最近更新 更多