8

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.

4
  • How do you define the idle state of Windows 7? Apr 27, 2012 at 20:07
  • Wish I knew. I've seen ways to supposedly hack that; the best answer I've found (and tested) is that the idle state seems to be triggered with 15 minutes of no user activity (keyboard or mouse etc. input) and little CPU usage. May 8, 2012 at 19:29
  • Just FYI, I think I found this Windows event to be very flaky (it didn't always respond to resumed user activity), and the goals I sought to accomplish with the question were far better accomplished by way of AutoHotkey, in a program I developed: github.com/r-alex-hall/farmComm Jun 14, 2014 at 7:20

1 Answer 1

5

I don't think it is possible to implement any methods to detect a trigger "border" on idle events(enter or exit from idle state), however there is a command that forcer your Windows to enter on idle state and run idle triggered tasks:

Rundll32.exe advapi32.dll,ProcessIdleTasks

You could combine another event(from event log) to run this command which in turn trigger another task that must be runned on idle state. As far you know you can freely combine any tasks that you may want.

1
  • 1
    Hmm . . . that leads me to think . . . I could approach it that way, without Rundll32.exe. I could set up scheduled taskA to run ProgA on computer idle--and then set up a TaskB, which runs batchB.bat once a minute. BatchB.bat would use Pslist to check if ProgA is running (and if it isn't, run ProgB). That by itself wouldn't work because ProgA would continue to run even if the computer leaves idle . . . BUT . . . (I just realized!) there's a task scheduler condition to "Stop [the program] if the computer ceases to be idle". If that kills execution of ProgA, then batchB.bat would run ProgB! Apr 27, 2012 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.