zoukankan      html  css  js  c++  java
  • 在批处理中实现等待/延迟/暂停

    (以下所有的程序均以等待/延迟/暂停2秒示例)

    1、比较传统的设计思路,利用for解析变量%time%并存为一个时间点,再利用set计算两个时间点的时间差,最后用if判断时间差是否达到设定的暂停时间。时间精度为0.01秒,适用平台为WinNT/2K/XP/2003。
      @echo off
      setlocal enableextensions
      echo %time%
      call :ProcDelay 200
      echo %time%
      goto :EOF

      :ProcDelay delayMSec_
      setlocal enableextensions
      for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
        :_procwaitloop
        for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
        set /a diff_=%now_%-%start_%
      if %diff_% LSS %1 goto _procwaitloop
      endlocal & goto :EOF

    2、使用Windows的VBS脚本中的sleep函数,可以动态创建这个VBS脚本,然后用Windows脚本宿主的命令行版本调用它。时间精度为0.001秒,使用平台为Win9x/WinNT系列。
      @echo off & setlocal enableextensions enabledelayedexpansion
      echo WScript.Sleep 2000 > %temp%\tmp$$$.vbs
      echo %time%
      cscript //nologo %temp%\tmp$$$.vbs
      echo %time%
      for %%f in (%temp%\tmp$$$.vbs) do if exist %%f del %%f
      endlocal & goto :EOF

    3、如果你的Windows系统中正常安装了网卡的TCP/IP协议,也可以使用ping定时发送测试包,以此达到暂停一定时间的目的;-n后的数字是发送包的数目,根据植树原则,为暂停秒数加一,此法每秒有0.5%的偏差。时间精度为1秒,使用平台为Win9x/WinNT系列。
    ping -n 3 127.0.0.1>nul

    4、使用choice的缺省选择等待功能实现暂停,但它有个缺点,就是不能在等待途中按键,否则暂停的倒计时将自动终止。时间精度为1秒,适用平台为MS-DOS/Win9x/WinNT系列。
    choice /t:y,2 /n >nul

    5、德国人Herbert Kleebauer给出了一个通用方案,通过间接产生一个exe程序来实现延迟,这个程序分为DOS和Win两个模块。延迟精度为0.001秒,适用平台为MS-DOS/Win9x/WinNT。代码见附件(http://willsort.blogchina.com/inc/sleep.bat.txt)。

  • 相关阅读:
    1、SpringBoot入门
    在一台电脑开启多个微信
    【监控】prometheus监控安装
    【hadoop3.0】hive 安装
    【google工具安装】gsutil存储管理google cloud stroge
    [监控报警]elastalert安装使用
    【大数据】hadoop3.0worker集群+flink+zeppelin+kafaka+zookeeper安装部署
    【原创】fluent-bit安装使用
    [etcd]etcd集群部署
    【手打】kafka集群设置
  • 原文地址:https://www.cnblogs.com/flyfish/p/482073.html
Copyright © 2011-2022 走看看