AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sleep Until Time
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Wed Mar 17, 2010 6:42 am    Post subject: Sleep Until Time Reply with quote

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 Fri Mar 19, 2010 10:41 pm; edited 2 times in total
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Wed Mar 17, 2010 8:53 am    Post subject: Reply with quote

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 Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
jenhafman
Guest





PostPosted: Wed Mar 17, 2010 10:15 am    Post subject: Reply with quote

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
}
}
Back to top
Guest






PostPosted: Wed Mar 17, 2010 12:39 pm    Post subject: Reply with quote

nice jenhafman

but small typo at the end "}" one too much?
Back to top
jenhafman
Guest





PostPosted: Wed Mar 17, 2010 2:13 pm    Post subject: Reply with quote

Anonymous wrote:
nice jenhafman

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

No, not a typo - sloppy copy & paste (and old eyes). Embarassed
Thanks for catching and reporting the error. Very Happy
Back to top
arsan



Joined: 02 Jan 2010
Posts: 105

PostPosted: Wed Mar 17, 2010 7:40 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Wed Mar 17, 2010 11:15 pm    Post subject: Reply with quote

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 Smile

arsan good searching I don't think Settimer would work in a functon very well.
Back to top
View user's profile Send private message
arsan



Joined: 02 Jan 2010
Posts: 105

PostPosted: Thu Mar 18, 2010 12:10 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Mar 18, 2010 3:09 pm    Post subject: Reply with quote

the sleep stops my other codes working
Back to top
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Thu Mar 18, 2010 4:29 pm    Post subject: Reply with quote

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.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
arsan



Joined: 02 Jan 2010
Posts: 105

PostPosted: Thu Mar 18, 2010 4:31 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Thu Mar 18, 2010 4:35 pm    Post subject: Reply with quote

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.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Thu Mar 18, 2010 5:09 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
arsan



Joined: 02 Jan 2010
Posts: 105

PostPosted: Thu Mar 18, 2010 5:49 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Zaelia



Joined: 31 Oct 2008
Posts: 604
Location: France

PostPosted: Fri Mar 19, 2010 11:22 am    Post subject: Reply with quote

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 Smile

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
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group