Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Wake-up-timer/ Scheduler-Function


  • Please log in to reply
9 replies to this topic
Boskoop
  • Members
  • 75 posts
  • Last active: Jan 11 2011 09:12 PM
  • Joined: 01 Jan 2005
AHK wake-up-timer/ scheduler provides some of the functions of Window's Task Scheduler or the command line tool "schtasks" as an AHK-script. Unlike Windows' Scheduled Tasks this function works with accounts without password.

The script can
1. Start a task at a given time
2. Wake the computer from standby at a given time to execute a task (optional)
3. Hibernate or suspend the computer (optional)

The script is testet on XPHome SP1 and XP Pro SP2. It should work on Windows98, Windows 2000 too. It's tested with AHK 1.0.44.7, but it should work with v1.0.36.07+. Wake up from Hibernate requires hardware support for S4-mode. You can find more info about standby-modes here

More than one timer
It's possible to start more than one timer, but every timer must be started in an own AHK-script.

Boskoop

-----------------------------------------------------
Edit 2006-08-08:
Made it a function

Edit 2006-08-30:
+ CloseHandle
+ can suspend the computer
+ wake up to execute a task now optional


-----------------------------------------------------
This is a working script. Just change the start time:

; CONFIGURATION 
Year=2006 
Month=8   ;1-12 
Day=30      ;1-31 
Hour=21   ;0-23 
Minute=16 ;1-59 
Hibernate=0 ;0, 1, 2
Resume=1 ;0,1
Name=%A_Now%

; AUTOEXECUTE 
WakeUp(Year, Month, Day, Hour, Minute, Hibernate, Resume, Name) 

msgbox, I'm awake and working! 


; FUNCTIONS 
WakeUp(Year, Month, Day, Hour, Minute, Hibernate, Resume, Name) 
;Awaits duetime, then returns to the caller (like some sort of "sleep until duetime"). 
;If the computer is in hibernate or suspend mode 
;at duetime, it will be reactivated (hardware support provided) 
;Parameters: Year, Month, Day, Hour, Minute together produce duetime 
;Hibernate: If Hibernate=1, the function hibernates the computer. If Hibernate=2 the computer is set to 
;			suspend-mode
;Resume: If Resume=1, the system is restored from power save mode at due time
;Name: Arbitrary name for the timer
{ 
    duetime:=GetUTCFileTime(Year, Month, Day, Hour, Minute) 

    Handle:=DLLCall("CreateWaitableTimer" 
            ,"char *", 0 
            ,"Int",0 
            ,"Str",name, "UInt") 

    DLLCall("CancelWaitableTimer","UInt",handle) 

    DLLCall("SetWaitableTimer" 
          ,"Uint", handle 
          ,"Int64*", duetime        ;duetime must be in UTC-file-time format! 
          ,"Int", 1000 
          ,"uint",0 
          ,"uint",0 
          ,"int",resume) 
    

    ;Hibernates the computer, depending on variable "Hibernate": 
    If Hibernate=1 		;Hibernate
        { 
        DllCall("PowrProf\SetSuspendState", "int", 1, "int", 0, "int", 0) 
        } 
        
    If Hibernate=2		;Suspend
    	{
    	DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0) 
    	}
    Signal:=DLLCall("WaitForSingleObject" 
            ,"Uint", handle 
            ,"Uint",-1) 
            
    DllCall("CloseHandle", uint, Handle)   ;Closes the handle
    
}    


GetUTCFiletime(Year, Month, Day, Hour, Min) 
;Converts "System Time" (readable time format) to "UTC File Time" (number of 100-nanosecond intervals since January 1, 1601 in  Coordinated Universal Time UTC) 
{ 
    DayOfWeek=0 

    Second=00 
    Millisecond=00 
    

    ;Converts System Time to Local File Time: 
    VarSetCapacity(MyFiletime  , 64, 0) 
    VarSetCapacity(MySystemtime, 32, 0) 
    
    InsertInteger(Year,       MySystemtime,0) 
    InsertInteger(Month,      MySystemtime,2) 
    InsertInteger(DayOfWeek,  MySystemtime,4) 
    InsertInteger(Day,        MySystemtime,6) 
    InsertInteger(Hour,       MySystemtime,8) 
    InsertInteger(Min,        MySystemtime,10) 
    InsertInteger(Second,     MySystemtime,12) 
    InsertInteger(Millisecond,MySystemtime,14) 

    DllCall("SystemTimeToFileTime", Str, MySystemtime, UInt, &MyFiletime) 
    LocalFiletime := ExtractInteger(MyFiletime, 0, false, 8) 

    ;Converts local file time to a file time based on the Coordinated Universal Time (UTC): 
    VarSetCapacity(MyUTCFiletime  , 64, 0) 
    DllCall("LocalFileTimeToFileTime", Str, MyFiletime, UInt, &MyUTCFiletime) 
    UTCFiletime := ExtractInteger(MyUTCFiletime, 0, false, 8) 
    
    Return UTCFileTime 
} 


ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 32) 
; Documented in Autohotkey Help 
{ 
    Loop %pSize%  
        result += *(&pSource + pOffset + A_Index-1) << 8*(A_Index-1) 
    if (!pIsSigned OR pSize > 4 OR result < 0x80000000) 
        return result  
    return -(0xFFFFFFFF - result + 1) 
} 



InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4) 
; Documentated in Autohotkey Help 
{ 
  Loop %pSize% 
          DllCall("RtlFillMemory", UInt, &pDest + pOffset + A_Index-1 
                  , UInt, 1, UChar, pInteger >> 8*(A_Index-1) & 0xFF) 
}


halllon
  • Members
  • 16 posts
  • Last active: Feb 08 2008 12:43 PM
  • Joined: 29 Nov 2007
This promises to be a great script for a project I am doing. :D

Is it possible to have a key press make the computer resume from sleep mode with this script?

  • Guests
  • Last active:
  • Joined: --
You might be interested in AHK-Scheduler, some sort of Windows Scheduled Tasks clone. AHK-Scheduler is using this function.

Is it possible to have a key press make the computer resume from sleep mode with this script?

My computer wakes up on any mouse movement or key press while in sleep mode. In hibernate mode, I have to push the ON-button. I don't know if its possible at all to wake a computer from hibernate mode by a key press.

Boskoop

Poolee
  • Guests
  • Last active:
  • Joined: --
Only if it's support by your BIOS. My last computer's BIOS had an option to allow any key or a password to wake the computer from off or hibernation.

Cheers
Paul

gahks
  • Guests
  • Last active:
  • Joined: --
Just what I was looking for, great. Works fine on XPSP2.

Cheers,
gahks

sabukz
  • Members
  • 33 posts
  • Last active: May 09 2013 06:13 AM
  • Joined: 15 Dec 2009
could one explain the above script
Cheers!

RandomUser
  • Guests
  • Last active:
  • Joined: --
I'm not extremely well versed in this, but I was using this script and having errors and not waking up at the correct time. The "minute" parameter is specified as "Min" (instead of "Minute") later in the script. Once I changed the "Min" parameter to "Minute", it worked great. Again, I'm not an expert and I'm not 100% sure this was the problem, but once I changed it the program worked fine.

Pooja
  • Guests
  • Last active:
  • Joined: --
hi,

I want to hibernate at 9Pm and wake up at 8.30am daily. how to run this program ? please help :cry:

kosame
  • Members
  • 1 posts
  • Last active: May 17 2013 06:57 AM
  • Joined: 16 May 2013
Basically, the scripts works well for me, except that , when the script is running, after pressing sleep button, I have to wait about 10 seconds for my computer turning into standby mode. Normally, my computer will standby as soon as I press sleep button. I don't know why.

Boskoop the author really did a nice work.

I noticed that he have devoted a large portion to the function GetUTCFiletime(). I'm a layman to compter science. So I cannot figure out what happened.

After spending some time googling, I wrote my own version of GetUTCFiletime().

-------------------

GetUTCFiletime(Year, Month, Day, Hour, Min)
{
GMT:=+8

yyyymmddHHmm:=Year*100000000+Month*1000000+Day*10000+Hour*100+Min

oldest_day:=160101010000

envsub, yyyymmddHHmm, oldest_day, Second

ticks_in_computer:=(yyyymmddHHmm-GMT*3600)*10000000

return ticks_in_computer

}

-------------------

It seems Windows treat time 2000.01.01 01hr01min as something like 125911332600000000 and 2000.01.01 01hr02min as 125911333200000000 internally.
Windows counts how many seconds elapsed since the very dawn of 1601.Jan.01. Then it add 7zeros, makes the number 10 million times bigger. That's how it gets those meaningless numbers.

Since I live in GMT +8 time zone, my computer thinks its clock is 8 hours ahead of European computers. So I have to minus 8*3600 before multiply 10 million.

My scripts may be stupid and far less efficient compared to Boskoop's, But it's a lot easier to read and write. I hope it will be helpful for newbies like me.

VesQ
  • Members
  • 5 posts
  • Last active: Feb 03 2015 05:57 PM
  • Joined: 15 Nov 2013

That doesn't work with windows 7. Can anybody say which line I have to modify?