批处理:如何在计算机空闲时运行程序并在使用时停止程序?

use*_*297 4 windows batch

我需要运行一个 Windows 批处理(或任何其他命令行软件都可以),它会在计算机空闲一分钟时运行一个程序,并在使用时停止它。当它再次空闲时,我显然应该重新启动它。

有任何想法吗?找不到任何不使用 GUI 的东西。

Ric*_*ard 7

如果您不想使用 AutoIt,请查看quser命令,其中显示空闲时间到最接近的分钟:

C:\Users\Richard>quser
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
>richard               console             1  Active      none   26/06/2014 15:29
C:\Users\Richard>
Run Code Online (Sandbox Code Playgroud)

一种简单的方法是测试以下输出:

quser | findstr /I %USERNAME% | findstr "none"

如果它返回空,则它们闲置超过 1 分钟。


Mic*_*ica 6

这是 AutoIt 的完美工作:http : //autoitscript.com

这是我为您编写的脚本。把它放在一个 .au3 文件中,用你的 exe 替换记事本,对于运行,包括完整路径:

#include <Timers.au3>
While 1
   Sleep(10)
   $idleTimer = _Timer_GetIdleTime()
   If $idleTimer > 60000 And Not ProcessExists("notepad.exe") Then
      Run("notepad.exe")
   ElseIf $idleTimer < 10 Then
      ProcessClose("notepad.exe")
   EndIf
WEnd
Run Code Online (Sandbox Code Playgroud)