The Windows 7 task scheduler allows me to run a task when the computer goes idle, but there doesn't seem to be any obvious way to run a task when the computer resumes from idle, or is no longer idle.
Surely there's some event triggered in windows (an event log?) when the computer is no longer idle? Or some way to capture the fact that the computer is no longer idle, and respond to that with a scheduled task?
How would I do this?
Or, at worst, is there a command-line program somewhere that can invoke commands or events when the computer enters/exits idle status?
[UPDATE:] The approach in my reply to Diogo Rocha works. I created a null executable via py2exe out of this script:
import sys
import time
#restart a pause every twenty seconds, with two functions that call each other.
def call_pause():
pause()
def pause():
time.sleep(20)
call_pause()
call_pause()
--and set up a scheduled task in Windows for which this is the exported HTML:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2012-04-27T17:40:46.8871631</Date>
<Author>GENIUS-BREATH-COMPY</Author>
<Description>This task runs ProgA when the computer enters an idle state, and terminates ProgA when the computer *leaves* an idle state. The is all for scheduled TaskB, which periodically runs a batch that tests whether ProgA is running. If ProgA is not running (because this task terminated it), ProgB runs (as the computer is NOT idle). If ProgA *is* running, TaskB's batch does not run ProgB.</Description>
</RegistrationInfo>
<Triggers>
<IdleTrigger>
<Enabled>true</Enabled>
</IdleTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<Duration>PT1M</Duration>
<WaitTimeout>PT0S</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>true</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>true</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT1M</Interval>
<Count>3</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\path_to\nullExecutable</Command>
</Exec>
</Actions>
</Task>
And left my computer idle for 15 minutes. Task manager showed the null executable running. As soon as I moved the mouse, it took the computer out of idle, and the null executable vanished from the task list.
From here, it's a matter of setting up a task (or program--which I'm doing with Python and py2exe) that uses pslist (with an -accepteula switch so that on computers it's deployed to it will actually run the program) to check whether the null exe is running. If it is running, the %ERRORLEVEL% environment variable is set to 0 because pslist ran without error. If that environment variable is 1, it ran with an error (it didn't find the executable running). I'm exploiting that environment variable in a batch script to run another task if the computer is not idle.