CMD 中的并行/多线程

发布于 2024-09-08 02:36:23 字数 394 浏览 9 评论 0原文

我需要同时运行计数器和计时器,但我不确定如何实现它。

我有一个批处理文件,可以计算在由 goto 制作的简单循环中按下任意键的次数, 第一次完成(按键)后,它会触发一个计时器 1 分钟; 当时按下的键必须存储在另一个变量中。

我的问题是我不知道如何使循环在计时器计数时继续运行,因为我尝试了两个选项但没有成功:

  • 在按键后调用(在同一个 CMD 窗口内,对我来说最好的)计时器会触发计时器,但它会等到计时器完成。
  • 在新窗口中启动计时器(我想到的办法是以防万一没有机会并行运行);为了使循环意识到计时器已完成,我尝试在之前和之后切换全局变量,但我无法设法使其在主窗口中保留提示窗口中设置的最后一个值(带有计时器的值) )。
  • 希望我正确地解释了自己,有人可以帮助我, 谢谢,丹。

    I need to run a counter and a timer at the same time, but I'm not sure about how to achieve it.

    I have a batch file that counts the number of times any key is pressed in an easy loop made by a goto,
    once its done (keypress) for the first time, it fires a timer for 1 min;
    the key pressed in that time, must be stored in another variable.

    My problem is that I don't know how to make the loop to continue running while the timer is counting, because I tried two options without success:

  • Calling (inside the same CMD window, the best for me) the timer after the keypress fires the timer, but it waits till timer has finished.
  • Starting the timer in a new window (the chice I thought about in case there's no chance of running both in parallel); and to be the loop aware that the timer finished, I tried switching a global variable before and after, but i can't manage to make it to keep in the main window the last value set in the prompt window (the one with the timer).
  • Hope I explained myself correclty and somebody can help me,
    thanks, Dan.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    百合的盛世恋 2024-09-15 02:36:23

    您可以在一个 cmd 会话中运行并行线程。
    使用带有 /B 参数的 start 命令,它将在当前 cmd 窗口中启动批处理。

    Start /B myBatch.bat param1 param2 ... 
    

    当 myBatch.bat 在后台运行(并输出到当前窗口)时,您可以继续工作。

    注意 ^C 不会杀死它,只会杀死它 ^Break。

    确保任务被终止的最简单方法是使用 exit 命令结束 myBatch.bat。

    You can run parallel threads in one cmd session.
    use start command with /B parameter, it will start your batch in the current cmd window.

    Start /B myBatch.bat param1 param2 ... 
    

    you can continue work while myBatch.bat runs in the background (and output to the current window).

    note ^C will not kill it, only ^Break.

    easiest way to make sure task is killed is to end myBatch.bat with exit command.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文