Jump to content

run program on wake from standby?


Recommended Posts

Does anyone know if winXP has something built in that can run a program when the computer wakes up from standby? (just like it does for startup?)

If not, is there a way to do this with Autoit?

Thanks!

Link to comment
Share on other sites

Hi,

what do you want to achieve? Maybe you can start program before "going to sleep" and then the program should still run if pc awakes.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

ok, I'll explain the situation a bit. I run a pvr on my comuter, and it wakes itself up to record tv shows. I want to run an autoit script that updates the router to forward a port to the pvr computer whenever it wakes up from standby.

I would rather that the script does not run all of the time, but only once when the coputer wakes up.

Any more ideas?

Link to comment
Share on other sites

Maybe this works:

Dim $check, $i = 0, $time = 60

$check = PixelChecksum( 0, 0, @DesktopWidth, @DesktopHeight)

While $check
    Sleep(1000)
    $i = $i + 1
WEnd

If $i > $time Then
;~  Your Code in here
        Exit
Else 
    Exit
EndIf


;~ $time = Your time in minutes after standby

You just have to run it before pcs goes to standby

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

  • Moderators

Maybe this works:

Dim $check, $i = 0, $time = 60

$check = PixelChecksum( 0, 0, @DesktopWidth, @DesktopHeight)

While $check
    Sleep(1000)
    $i = $i + 1
WEnd

If $i > $time Then
;~  Your Code in here
        Exit
Else 
    Exit
EndIf
;~ $time = Your time in minutes after standby

You just have to run it before pcs goes to standby

Have you tried that on anything?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

No i dont know how standby works and so ^^

try it out yourself

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

  • Moderators

No i dont know how standby works and so ^^

try it out yourself

That wasn't my point. You only check for a pixel before the While/WEnd loop, then never check it again, how is the While/WEnd supposed to know if $check is different? That's all I was saying, you have an infinite loop that will never exit. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Developers

Give this a try ... requires BETA:

#include <GUIConstants.au3>
;~ #define PBT_APMQUERYSUSPEND             0x0000
;~ #define PBT_APMQUERYSTANDBY             0x0001
;~ #define PBT_APMQUERYSUSPENDFAILED       0x0002
;~ #define PBT_APMQUERYSTANDBYFAILED       0x0003
;~ #define PBT_APMSUSPEND                  0x0004
;~ #define PBT_APMSTANDBY                  0x0005
;~ #define PBT_APMRESUMECRITICAL           0x0006
;~ #define PBT_APMRESUMESUSPEND            0x0007
;~ #define PBT_APMRESUMESTANDBY            0x0008
;~ #define PBTF_APMRESUMEFROMFAILURE       0x00000001
;~ #define PBT_APMBATTERYLOW               0x0009
;~ #define PBT_APMPOWERSTATUSCHANGE        0x000A
;~ #define PBT_APMOEMEVENT                 0x000B
;~ #define PBT_APMRESUMEAUTOMATIC          0x0012
;
Global $WM_POWERBROADCAST     = 536
Global $PBT_APMRESUMESUSPEND  =  0x0007

$hGUI       = GUICreate("Test", 100, 100,1,1)
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")
GUISetState()
While 1
    $GUIMsg = GUIGetMsg()
    Switch $GUIMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd
;
Exit
;
Func Standby($hWnd, $Msg, $wParam, $lParam)
    If $wParam = $PBT_APMRESUMESUSPEND Then
        MsgBox(0,"Hello Back", " You just woke up")
    EndIf
EndFunc
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Edit 1: Height of combobox to 120 for W2k

Tested only Windows XP Sp2

Hello all i've modified one of my first program called minialarm

http://www.autoitscript.com/forum/index.ph...518entry74518

Now it can wake up the pc and start a program at select time :(

TIP: Set your Poweroption to no Password resume from standby

code not clean but it works :)

; AutoIt Version: 3.1.1.103 Beta
; Author:  Micha1405
; Date: 14.03.2005
; Script Function: Alarmtimer & sheduler start application or shutdown
; when the gui lost focus it minimize to tray
; 21.05.2006 add wake from standby and run application after standby
; ----------------------------------------------------------------------------

; Set Variablen
#include <GUIConstants.au3>
#include <constants.au3>
#include <date.au3>
#include<Date.au3>
Global $Sec, $Min, $Hour, $Time
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
Opt("GUICloseOnESC", 0)
opt("TrayAutoPause",0)
$WinTitel="MiniAlarm"
$aFlag=0;Alarm Timer Flag 0=not activ 1=play sound 2=start app 3=shutdown 4=wakeup @time 5=wakeup @time and start app
$font="Comic Sans MS"

; -----------------------------------------------------------------------------
; Build GUI 
$guiApp=GUICreate($winTitel, 200, 150)
GUISetFont (14, 400, 0, $font) 
$currTime=GUICtrlCreateLabel(@Hour & ":" & @Min & ":" &@SEC , 25, 8)
$font="Arial"
GUISetFont (8, 400, 0, $font) 
$alarmbutton = GUICtrlCreateButton("Set Alarm Time", 10, 120, 110,20)
$aCombo=GUICtrlCreateCombo ("Alarm", 10,60,150,120); create first item
$alabel1=GUICtrlCreateLabel ("Choose Action", 10,35,120);

GUICtrlSetData($aCombo,"Start App|Shutdown PC|Wake from Standby|Wake from Standby & run","Alarm"); add other item snd set a new default
$Apps = GUICtrlCreateLabel ( "Start @", 10,  90, 145, 20)
GUISetIcon ("shell32.dll",20) 
TraySetIcon("shell32.dll",20)
TraySetToolTip("Click to restore")
GUISetState()
$appState = @SW_SHOW
Do
  $msg = GUIGetMsg()
  $trayMsg  = TrayGetMsg()
        if Not WinActive($winTitel) And $appState = @SW_SHOW Then;lost focus minimize window
        _ToggleAppState()
        EndIf
   Select
        case $trayMsg = $TRAY_EVENT_PRIMARYDOWN
            _ToggleAppState()
    
        Case $msg = $alarmbutton
            $varT=InputBox("Set Time", "Set Time 'hh:mm':","00:00")
            if @error then
            MsgBox(64,"Error","Input canceled")
            $aFlag=0
            GUICtrlSetState($aCombo,$GUI_Enable) 
            Else
;Check for valid input
                If StringLen($varT)<5 or StringMid($varT, 3, 1)<> ":"then 
                    MsgBox(64,"Miniarlam","Incorrect format : 01:05")
                $aFlag=0
                Else
                $aFlag=1;set timer flag activ
                GUICtrlSetData($alarmbutton, "Alarm at " & $varT)
                GUICtrlSetState($alarmbutton,$GUI_DISABLE) 
                GUICtrlSetState($aCombo,$GUI_DISABLE)
    ;if Standby is active then get time -1 min and set Standby time
                
                EndIf
            EndIf
;check action to do, read value of combobox
            $ComboValue=GUICtrlRead($aCombo,0)
        Select
                Case $ComboValue="Alarm" and $aFlag>0
        ;do Nothing
        ;$aFlag=1
                Case $ComboValue="Start App"and $aFlag>0  OR $ComboValue="Wake from Standby & run" and $aFlag>0
                    GUISetState($Apps,$GUI_enable)
                    $a_apps=FileOpenDialog("Choose Applikation to start at alarmtime",@ProgramFilesDir, "Apps (*.exe;*.cmd)", 1 + 4)
                            If @error Then
                                MsgBox(4096,"","No App select")
                                $aFlag=0
                                $fvar = EnableButtons()
                            Else
                            GUICtrlSetData($apps,$a_apps)
                            GUICtrlSetData($alarmbutton,"Start @ " &$varT)                      
                        EndIf
                        if $ComboValue="Wake from Standby & run" Then
                            $aflag=5
                ;reduce time for 2 minutes for systemstartup
                            $Sec="00"
                            $Min=StringRight($varT,2)
                            $Hour=StringLeft($varT,2)
                            $var=_TimeToTicks($Hour,$Min,$Sec)
                            $var1=$var -2 * 60 * 1000
                            _TicksToTime($var1,$Hour,$Min,$Sec)
                            SetWakeUpTime($Hour,$Min); wakeup the system
                            Sleep(1000)
                            SetSuspend()
                            Else
                            $aFlag=2
                        EndIf
                case $ComboValue="Shutdown PC" and $aFlag>0
                    $aFlag=3
                    Case $ComboValue="Wake from Standby" and $aFlag>0
                    $aFlag=4
                    SetWakeUpTime(Stringleft($varT,2),StringRight($varT,2)); wakeup the system
                    Sleep(1000)
                    SetSuspend()
        EndSelect   

    EndSelect
        SLEEP(60)
        GuiCtrlSetData($currTime,  @Hour & ":" & @Min & ":" & @Sec)
        if $Aflag >=1 Then
            if @Hour & ":" & @Min = $varT then  
            Select
                case $aflag=1
                    If FileExists(@ScriptDir & "\alarm.wav") Then
                        SoundPlay(@ScriptDir & "\alarm.wav",0)
                        Else
                        MsgBox(64,$winTitel,"alarm.wav missing in programdir",3)
                    EndIf
                    $aFlag= 0; Set Timer Flag back to 0
                    $fvar = EnableButtons()
                Case $aflag=2 or $aflag=5
        ;Start apps
                    Run ( $a_apps )
                    $aflag=0
                    $fvar = EnableButtons()
                Case $aflag=3
                    $start=RunWait(@ComSpec & " /c " & 'shutdown -s -t 00', "", @SW_HIDE);shutdown
                    $aflag=0
                    $fvar = EnableButtons()
            EndSelect
        EndIf
    EndIf
until $msg = $GUI_EVENT_CLOSE

;---------------------------------------------------------------
; Userdefined Functions
Func EnableButtons()
            GUICtrlSetState($alarmbutton,$GUI_Enable) 
            GUICtrlSetData($alarmbutton, "Set Alarm Time")
            GUICtrlSetState($aCombo,$GUI_Enable)
            GUICtrlSetData($apps,"Start @")
EndFunc
        
; User defined function minimize and restore the gui
Func _ToggleAppState()
    if $appState = @SW_HIDE then
        $appState = @SW_SHOW
        GUISetState(@SW_SHOW, $guiApp)
    Else
        $appState = @SW_HIDE
        GUISetState(@SW_HIDE, $guiApp)
    EndIf
EndFunc

;===============================================================================
;
; Description:    Sets a wakeup time to wake it up if the system / computer is hibernating or standby
; Parameter(s):  $Hour   - Hour Values  : 0-23
;                   $Minute - Minutes Values: 0-59
;                   $Day    - Days Values   : 1-31  (optional)
;                  $Month    - Month Values : 1-12   (optional)
;                  $Year     - Year Values  : > 0    (optional)
;
; Requirement(s):   DllCall
; Return Value(s):  On Success - 1
;                  On Failure - 0 sets @ERROR = 1 and @EXTENDED (Windows API error code)
;
; Error code(s):     [url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp]http://msdn.microsoft.com/library/default....error_codes.asp[/url]
;
; Author(s):        Bastel123 aka Sebastian
; Note(s):        -
;
;===============================================================================
func SetWakeUpTime($Hour,$Minute,$Day=@mday,$Month=@mon,$Year=@YEAR)

$SYSTEMTIME = DllStructCreate("ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort")
$lpSYSTEMTIME = DllStructGetPtr($SYSTEMTIME)
$LOCALFILETIME=DllStructCreate("dword;dword")
$lpLOCALFILETIME = DllStructGetPtr($LOCALFILETIME)
$DueTime=DllStructCreate("dword;dword")
$lpDueTime=DllStructGetPtr($DueTime)

DllStructSetData($SYSTEMTIME, 1, $Year)
DllStructSetData($SYSTEMTIME, 2, $Month)
DllStructSetData($SYSTEMTIME, 3, _DateToDayOfWeek($Year,$Month,$Day)-1)
DllStructSetData($SYSTEMTIME, 4, $Day)
DllStructSetData($SYSTEMTIME, 5, $Hour)
DllStructSetData($SYSTEMTIME, 6, $Minute)
DllStructSetData($SYSTEMTIME, 7, 0)
DllStructSetData($SYSTEMTIME, 8, 0)

$result = DllCall("kernel32.dll", "long", "SystemTimeToFileTime", "ptr", $lpSystemTime, "ptr", $lpLocalFileTime)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
$result = DllCall("kernel32.dll", "long", "LocalFileTimeToFileTime", "ptr", $lpLocalFileTime, "ptr", $lpLocalFileTime)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
$result = DllCall("kernel32.dll", "long", "CreateWaitableTimer", "long", 0, "long", True, "str", "")
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
DllCall("kernel32.dll", "none", "CancelWaitableTimer", "long",$result[0])

DllStructSetData($DueTime, 1, DllStructGetData($LocalFILETIME, 1))
DllStructSetData($DueTime, 2, DllStructGetData($LocalFILETIME, 2))

$result = DllCall("kernel32.dll", "long", "SetWaitableTimer", "long",$result[0], "ptr", $lpDueTime, "long", 1000, "long", 0, "long", 0, "long", true)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
return 1
EndFunc

;===============================================================================
;
; Description:    Set the computer in Hibernate or Standby Status
; Parameter(s):  $Mode   - Suspend mode : True=Hibernate, False=Suspend
;                   $Force  - Force-Mode    : True=the system suspends operation immediately
;                                             False=FALSE, the system broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation 
;
; Requirement(s):   DllCall
;
; Author(s):        Bastel123 aka Sebastian
; Note(s):        If the system does not support hibernate use the standby mode       -
;
;===============================================================================
Func SetSuspend($mode=False,$force=true)
    $result = DllCall("PowrProf.dll", "long", "SetSuspendState", "long",$mode, "long",$force, "long", false)
EndFunc
Edited by Micha1405
Link to comment
Share on other sites

  • 2 months later...

Global $WM_POWERBROADCAST     = 536
Global $PBT_APMRESUMESUSPEND  =  0x0007
GUIRegisterMsg($WM_POWERBROADCAST, "Standby")
Func Standby($hWnd, $Msg, $wParam, $lParam)
    If $wParam = $PBT_APMRESUMESUSPEND Then
        MsgBox(0,"Hello Back", " You just woke up")
    EndIf
EndFunc
JdeB,

THANKS! I was just looking for this very thing (for a similar reason: want to do something with my router on resume).

Link to comment
Share on other sites

  • 16 years later...

Hi guys, I had this problem for the last several days.
I have a script that executes some timers and if (timer > value) + (computer has been idle > value ) then it should execute something.
What messes up everything is when windows goes to sleep/hibernate. Idle timers continue to run while the pc sleeps and on waking up windows the script triggers when it shouldn't.

The solution in the post above didn't work for me, the powerbroadcast = 536 didn't work on Windows 10.
I also read in another post that it's possible to fetch the windows even registry and find out the system resume time, but it was too complicated for my abilities and I didn't find any working example. So here is what I came up with, hoping that it will help anyone in my situation:
 

First workaround that I found:
Trigger only if idle time is between target time and target time + 1 second.
Let's say after 2 hours that the computer is idle you want to trigger a script. 2 hours = 7200 seconds.

If $mytimer > value and $idletime >7200 and $Idletime <7201 seconds Then
Execute what you want
sleep 3 seconds (to avoid repeating triggers if the script reloads in < 1 second)
EndIf

This means that if you hibernate windows and resume let's say after 3 hours, the 3rd condition won't be met and when you move the mouse the counter will resume from 0.
But: this is not elegant 😛

 

FINAL SOLUTION that I found later:
What I did:
Created a new au3 script. Only 1 instruction: Sleep 120 seconds. Called it: system_just_resumed.exe
This script is triggered by the windows task scheduler on system logon and on system resume after hybernation/sleep.


What I did in my main script: 

If $mytimer > value and $idletime >7200 and Not ProcessExists("system_just_resumed.exe") Then
Execute what you want
EndIf


Rationale: even if the pc has been idle while sleeping, the execution will be blocked by the system_just_resumed script for 2 minutes after resuming windows..
Which means: I have 2 minutes to move the mouse/keyboard after resuming the system to avoid a false trigger.

Hope this helps someone with my same problem I had.

Ps: in task scheduler, in order to trigger the exe file on system resume:

Quote

Open Task Scheduler and click on “Create a Task” to create a new task.
In the new panel which opens, fill the “General” tab to suit your needs.
Next, switch to the “Triggers” tab and choose “New”. Another Panel will open. At the top of this Panel, choose “On an event” from the drop-down menu.
Then choose “Custom” under Settings, and hit “New Event Filter”. Create a “Custom Event Filter” with:
Event Level: Information
Tick the radio button: "by source"
Source: Power-Troubleshooter
Event ID: 1

Source: https://sumtips.com/how-to/run-program-windows-wakes-up-sleep-hibernate/

 

Link to comment
Share on other sites

I might be wrong but there is a .dll for it <AutoItX3.dll>

example:
 

#include <AutoItX3.dll>

; Initialize AutoIt
_AutoItX_Startup()

; Create a scheduled task to run the program after wakeup
$sTaskName = "RunAfterWake"
$sProgramPath = "C:\Path\To\Your\Program.exe"

; Set the trigger for the task (on computer wake)
$sTrigger = '<QueryList><Query Id="0" Path="System"><Select Path="System">*[System[(EventID=1)]]</Select></Query></QueryList>'

; Set the action to run the program
$sAction = 'cmd /c "' & $sProgramPath & '"'

; Create the scheduled task
$nTaskCreated = _AutoItX_ScheduledTaskCreate($sTaskName, $sTrigger, $sAction)

; Check if the task was created successfully
If $nTaskCreated = 0 Then
    MsgBox(16, "Error", "Failed to create scheduled task.")
ElseIf $nTaskCreated = 1 Then
    MsgBox(64, "Success", "Scheduled task created successfully.")
EndIf

; Shutdown AutoIt
_AutoItX_Shutdown()


let me know if it worked 

Edited by 20Ice18

❤️

Link to comment
Share on other sites

This thread is more than 16 years old. A lot has changed in AutoIt since.
So I suggest to create a new thread.


@20Ice18

"AutoItX is a DLL version of AutoIt v3 that provides a subset of the features of AutoIt via an ActiveX/COM and DLL interface. This means that you can add AutoIt-like features to your favourite scripting and programming languages, e.g. VB, VBScript, Delphi, C, C++, Kixtart, and most other languages that support the use of DLLs."

I do not see the need to use AutoItX here.
Did you try to run your example? It returns a lot of errors. Example scripts posted here should at least pass the syntax check!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

1 hour ago, 20Ice18 said:

I might be wrong but there is a .dll for it <AutoItX3.dll>

example:
 

#include <AutoItX3.dll>

; Initialize AutoIt
_AutoItX_Startup()

; Create a scheduled task to run the program after wakeup
$sTaskName = "RunAfterWake"
$sProgramPath = "C:\Path\To\Your\Program.exe"

; Set the trigger for the task (on computer wake)
$sTrigger = '<QueryList><Query Id="0" Path="System"><Select Path="System">*[System[(EventID=1)]]</Select></Query></QueryList>'

; Set the action to run the program
$sAction = 'cmd /c "' & $sProgramPath & '"'

; Create the scheduled task
$nTaskCreated = _AutoItX_ScheduledTaskCreate($sTaskName, $sTrigger, $sAction)

; Check if the task was created successfully
If $nTaskCreated = 0 Then
    MsgBox(16, "Error", "Failed to create scheduled task.")
ElseIf $nTaskCreated = 1 Then
    MsgBox(64, "Success", "Scheduled task created successfully.")
EndIf

; Shutdown AutoIt
_AutoItX_Shutdown()


let me know if it worked 

My first time I'm seeing that it is possible to #include a DLL file directly in a script 👀.
This code looks like it's generated by an AI with hallucinated context and you made it worse by just straight copy-paste it as a solution to others.

Edited by sylremo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...