Idle time.

Posted by Ochbad on Fri 25 Feb 2005 05:05 AM — 4 posts, 27,074 views.

#0
Is it possible somehow (with VBscript) to determine the idle time of the computer?

I would like to make a script that does something when the computer is idle, and something else when the user comes back.
Russia #1
You can use WMI for that. I am not exactly sure that the values of the following property can be interpreted as indicator of "idleness" - it just shows the average amount of time the processor spent outside the idle thread lately:


set proc = GetObject("winmgmts:").InstancesOf ("Win32_PerfFormattedData_PerfOS_Processor")
  for each p in proc
    WScript.Echo p.PercentProcessorTime
  next


I am also unsure of what the individual members of that collection are: I have 2 logical processors and the collection for my system consists of 3 elements, so one of those elements either represents an average on two processors or the value for the physical processor, or something else. Perhaps, a simple average of all elements will do:


function IsIdle()
  dim total, count, procs, average
  set procs = GetObject("winmgmts:").InstancesOf("Win32_PerfFormattedData_PerfOS_Processor")
  total = 0
  count = 0
  for each proc in procs
    total = total + proc.PercentProcessorTime
    count = count + 1
  next
  average = total/count
  if average < 5 then
    IsIdle = vbTrue
  else
    IsIdle = vbFalse
  end if
end function
Greece #2
I think it would be easier for you to get the cursor position every 3 minutes or so and see if it has changed. If it has, the user probably is at the PC...
#3
I was so busy looking for a prepackaged solution I didn't even think of that simple cursor method. Should work!

Thanks!