AutoHotkey Community

It is currently May 27th, 2012, 5:45 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Sleep Until Time
PostPosted: March 17th, 2010, 7:42 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
People often want to have a script wait for a specific time to continue executing. This is usualy done with a loop that checks the current time against the destination time. I thought it would be better to use the current time to calculate a sleep time to get there. Then the Script only has to check the time once.
Function:
Code:
SleepTill(Time) {
ST_Hour:=SubStr(Time, 1 ,2)
ST_Min:=SubStr(Time, 3 ,2)
ST_Sec:=(SubStr(Time, 5 ,2)<>"" ? SubStr(Time, 5 ,2) : "00")
STime:=(((ST_Hour-A_Hour)*60+(ST_Min-A_Min))*60+(ST_Sec-A_Sec))*1000
STime:=STime<0 ? STime+86400000 : STime
Sleep %STime%
Return % A_Hour ":" A_Min
}

Examples:
Code:
SleepTill(1700)
MsgBox It is Now 5:00PM Time to go home.

MsgBox % "It is Now 30 sec's past "SleepTill(154830)

Modified version that can be used with SetTimer msTill

Edit:Added support for seconds and return time
Edit 2: added Link to Altered form


Last edited by None on March 19th, 2010, 11:41 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 9:53 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Why not make a sleep/runat library? SleepBetween, SleepAfter and some repeat parameters e.g. Run at 17:00 every (week)day/week etc...

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 11:15 am 
This is the code I wrote. Same number of lines (and a lot less math), but can use 4 or six digit time and optionally returns the final time.
Code:
; example useage
MsgBox % "It is Now " WaitForTime(0241) " Time to go home."   ; can use 4 digit time
MsgBox % "It is Now " WaitForTime(024130) " Time to go home."   ;  or 6 digit time
WaitForTime(0210)
now := WaitForTime(0210)

WaitForTime(eTime) {
  eTime := A_YYYY A_MM A_DD SubStr(eTime, 1 ,2) SubStr(eTime, 3 ,2) (SubStr(eTime, 5, 2) ? SubStr(eTime, 5, 2) : "00")
  if (A_Now > eTime) ; if eTime is past, set for tomorrow at eTime
    eTime += 1, day
  TimeToSleep := eTime
  TimeToSleep -= A_Now, seconds
  Sleep, % TimeToSleep * 1000
  FormatTime, current, A_Now, HHmmss ; optional - comment out if you don't need the current time returned
  Return, current
}
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 1:39 pm 
nice jenhafman

but small typo at the end "}" one too much?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 3:13 pm 
Anonymous wrote:
nice jenhafman

but small typo at the end "}" one too much?

No, not a typo - sloppy copy & paste (and old eyes). :oops:
Thanks for catching and reporting the error. :D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 8:40 pm 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
Here's something that was posted by Chris himself a while back to run a script at a certain time. It also uses the current time to calculate a waiting time to get there - the Script only has to check the time once.

Instead of Sleep, it uses SetTimer to wait till the calculated time-difference between the scheduled time and the current time.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 12:15 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
Thanks jenhafman returning time is a good idea
I thought adding seconds was silly but since you did it I figured why not.
I also changed the math so there arn't so many scary big numbers :)

arsan good searching I don't think Settimer would work in a functon very well.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 1:10 am 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
None wrote:
... arsan good searching I don't think Settimer would work in a functon very well.
You're right. It wasn't meant to be used as a function. It was just a demonstration of a criteria you mentioned - using the current time to calculate a wait time to get there so that the script has to check the time once only.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 4:09 pm 
the sleep stops my other codes working


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 5:29 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Anonymous wrote:
the sleep stops my other codes working

That's why SetTimer is usually a better solution.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 5:31 pm 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
Anonymous wrote:
the sleep stops my other codes working

You must post your code so that others may look into it to figure out the problem.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 5:35 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Related: Sleeper

arsan wrote:
You must post your code so that others may look into it to figure out the problem.

Not really. Sleep causes the script to hang, so it's a pretty obvious problem.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 6:09 pm 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
It Should only sleep the current thread. New threads created by SetTimer or Hotkeys should work.
I just tried this
Code:
SetTimer Try, 1000
SleepTill(100100)
MsgBox Hello
Return

a::MsgBox It Works
Esc::ExitApp

Try:
SoundBeep
Return
The Hotkeys worked and so did the SetTimer.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 6:49 pm 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
MasterFocus wrote:
Related: Sleeper

arsan wrote:
You must post your code so that others may look into it to figure out the problem.

Not really. Sleep causes the script to hang, so it's a pretty obvious problem.

The fact that Sleep waits the specified amount of time before continuing is so basic that I don't think that the poster won't know about it. That's why I wanted to have a look at his code.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 12:22 pm 
Offline
User avatar

Joined: October 31st, 2008, 9:39 am
Posts: 641
Location: France
Hi I'm author of Sleeper, sorry it's not a very good script and at this time I didn't know concept like scheduler or dispatcher... In fact, Sleeper thread is not famous :)

However, I think that my script can be customize, must replace the comparison of a_tickcount by a date seems easy...

other link, can be usefull ( it's the wish list) :
http://www.autohotkey.com/forum/viewtopic.php?t=22530


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: HotkeyStick, JamixZol, Yahoo [Bot] and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group