Computer refuses to sleep

sm625

Diamond Member
May 6, 2011
8,172
137
106
This computer has been irritating me for a while now. Its been running for almost 4 years. 3 years with windows 7. It used to be fairly reliable in the way it slept and resumed. Now, ever since I installed a M4 SSD, it rarely goes to sleep by itself. It still does sleep, but most of the time I see it does not.

I've gotten to the point where I'm just sick of how unreliable sleep is with windows, and have accepted the fact that it will never not ever work properly. So I've decided to write my own routine so I will have something that I know will work.

All I need my routine to do is check to see if the mouse hasnt been moved and no keys have been pressed for 10 minutes.

Here is the autohotkey code which does this check:

Code:
#Persistent
Settimer, Idle, 5000
return

idle:
if A_TimeIdlePhysical > 600000 
    MsgBox, idle
Else
    return

If it is truly idle then as a final check I need to somehow tell if there is any video playing, because that could be a possibility. That's kind of where I'm stuck at the moment. Since microsoft is trash I figured somebody somewhere must have dealt properly with this issue. So how do I tell if there is a video playing?
 
Last edited:

sm625

Diamond Member
May 6, 2011
8,172
137
106
I have decided to call netstat and have it create a log file that I can parse to find if any video is running. I'm going to use the change in total bytes received to determine if there is a video streaming. So when I'm finished I will have a simple script that will put computer to sleep if mouse and kb are idle and bytes received in 10 minutes is less than 100k-500k. (I havent decided on where to draw that line.)
 

theevilsharpie

Platinum Member
Nov 2, 2009
2,322
14
81
I've had issues with Windows 7 going to sleep because it thinks I'm sharing media. There's a setting to ignore it and go to sleep anyway in the Windows power control panel.
 

readymix

Senior member
Jan 3, 2007
357
1
81
same story here. pc won't stay in sleep mode for 2 minutes with a samsung ssd but there is no problem staying in sleep mode when using a wd 640 hard drive. with the wd 640 being a clone of the samsung.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
I think getting it to stay in sleep mode is relatively easy. Just disable wake timers, and disable that media sharing option that sharpie was talking about. Make sure the mouse cant wake it because any slight vibration will make a mouse wake a computer.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
Alright I think I have it. I been testing it on two machines and it seems to be working fine. What it does is check for audio output in addition to mouse moves and key presses. It will not issue the sleep command until all are absent.

I decided not to bother monitoring network activity, mainly because I dont care if my machine goes to sleep during some sort of transfer. I figure if the transfer is important then I'll probably be either typing or moving my mouse anyway.

Here is the autohotkey script in its entirety:

Code:
;To use this script you must first be running AutoHotkey L 110703 or newer to guarantee compatibility
; (Choose the unicode 32 bit version when installing)
;AND you must download VA21.zip from:
; http://www.autohotkey.com/community/viewtopic.php?f=2&t=23792
; Once VA21.zip is downloaded, locate your Autohotkey folder and create a subfolder called "Lib"
; Place VA.ahk in that Lib folder
;
;Note: This will not work on win XP or older, since it uses the vista audio whatchamacallit

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
#SingleInstance, Force
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;This variable controls how many minutes of idle time are needed before sleeping
idletime := 10

;Convert minutes to milliseconds
idletime *= 1000*60

AudioIdleCount := 0

;Setup the timer 

;Set the timer to such a length that will allow us to check the sound 
; output level 6 times during the 2nd half of the idle period
; note that we're going to skip audio monitoring during the first half of the idle period

slice := idletime / 12

Settimer, IdleTimer, %slice%



;the next 3 lines are debug code

;debugtime := 0
;TrayTip, Idletest, Idletest`nDebug Info, 20, 17
;SetTimer, RemoveTrayTip, 5000




return


IdleTimer:

;Force AudioIdleCount to zero for the first half of the idle period
if(A_TimeIdlePhysical < (slice*6))
{
  AudioIdleCount := 0
  return
}

;Check for mouse/kb input during 2nd half of idle period
if(A_TimeIdlePhysical < (slice*(AudioIdleCount+6)))
{
  return
}

;continue only if mouse/keyboard are idle


audioMeter := VA_GetAudioMeter()

VA_IAudioMeterInformation_GetMeteringChannelCount(audioMeter, channelCount)

; "The peak value for each channel is recorded over one device
;  period and made available during the subsequent device period."
VA_GetDevicePeriod("capture", devicePeriod)

; Get the peak value across all channels.
VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue) 
   
if(peakValue < 0.00001)
{
    AudioIdleCount++
}
else
{
    AudioIdleCount := 0  ;we still have some sound playing, so reset the counter
}

if(AudioIdleCount < 7)
  return

;MsgBox, idle!

;Set this back to zero so it will stay awake once it wakes back up!
AudioIdleCount := 0

  
; Call the Windows API function "SetSuspendState" to have the system suspend or hibernate.
; Windows 95/NT4: Since this function does not exist, the following call would have no effect.
; Parameter #1: Pass 1 instead of 0 to hibernate rather than suspend.
; Parameter #2: Pass 1 instead of 0 to suspend immediately rather than asking each application for permission.
; Parameter #3: Pass 1 instead of 0 to disable all wake events.
DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0)

return


;debugging code

RemoveTrayTip:
SetTimer, RemoveTrayTip, Off
TrayTip
;return

; To have a TrayTip permanently displayed, use a timer to refresh it periodically:
SetTimer, RefreshTrayTip, 1000
Gosub, RefreshTrayTip  ; Call it once to get it started right away.
return

RefreshTrayTip:
debugtime := A_TimeIdlePhysical / 1000
TrayTip, Idletest Debug Info, A_TimeIdlePhysical = %debugtime%`nAudioIdleCount = %AudioIdleCount%, , 16
return
 
Last edited:

sm625

Diamond Member
May 6, 2011
8,172
137
106
I've been using this script for a few days now and it has been working great. One other thing I want to point out is that a wireless keyboard could wake your computer even when you never press a key. It sucks to have to reach down and press the power button to wake the computer. I'm thinking of installing a motion sensor under the desk. :D