Zohar Posted June 11, 2009 Share Posted June 11, 2009 (edited) HelloThis is a syntax question.Let's say you have this block of code in AutoIt Script:Local $Timer=TimerInit() Do $oIE =_IEAttach($String,$Mode) Sleep(500) Until( (@error=0) Or (TimerDiff($Timer)>15000) )Thos block is working great.What I want to do, is to put the Sleep(500) inside the Loop's Condition.The problem is that Sleep() does not return anything.Is there a way that I can still put it in the Condition line?that way I can put it in the end of the line, which means the Sleep(500) will be performed only if it's needed.Thank you Edited June 11, 2009 by Zohar Link to comment Share on other sites More sharing options...
MrMitchell Posted June 11, 2009 Share Posted June 11, 2009 Your Sleep() is setting @error to 0... so assign @error to another variable inside the loop then use that new variable in the loop condition? Link to comment Share on other sites More sharing options...
Zohar Posted June 11, 2009 Author Share Posted June 11, 2009 Sleep() does not touch @error.@error is being affected only by _IEAttach().The code that I wrote is working.what I try to do is to put the Sleep(500) in the Condition line, and still make it work. Link to comment Share on other sites More sharing options...
MrMitchell Posted June 11, 2009 Share Posted June 11, 2009 Ohhh ok, sorry about that. Just trying to figure out why Sleep() doesn't work where it is at now Link to comment Share on other sites More sharing options...
Inverted Posted June 11, 2009 Share Posted June 11, 2009 (edited) Sleep isn't a condition/expression ... that makes no sense actually :-( Edited June 11, 2009 by Inverted Link to comment Share on other sites More sharing options...
Zohar Posted June 11, 2009 Author Share Posted June 11, 2009 Here's a simplified block, for others to be able to test it:Local $E=1 Local $Timer=TimerInit() Do ConsoleWrite("hi" & @CRLF) Until( _ ($E=0) Or _ ( (TimerDiff($Timer)>3000) Or Sleep(500) ) _ )This small block, has the Sleep(500) in the condition line, and is not working.what do I need to do, to make it work?(apart from moving the Sleep(500) back to the code lines of course) Link to comment Share on other sites More sharing options...
Zohar Posted June 11, 2009 Author Share Posted June 11, 2009 (edited) Inverted:It's a trick used in many programming languages, to put the Sleep() function in the condition, after the other conditions,that way the Sleep() is performed only if the loop is going to continue looping. Edited June 11, 2009 by Zohar Link to comment Share on other sites More sharing options...
MrMitchell Posted June 11, 2009 Share Posted June 11, 2009 I guess you could try to put another function call in the condition? Put the sleep and all other calculations and comparisons in the new function? Link to comment Share on other sites More sharing options...
Zohar Posted June 11, 2009 Author Share Posted June 11, 2009 (edited) MrMitchell:your idea will definately work, but will make the code not so simple and short..I would like to avoid creating another function every time I need this.Inverted:What you suggest, is equivalent to my original code, that I posted in the first post.The Sleep(500) is performed anyway, even if the loop should stop..BTWIt seems like despite the fact that the help says that Sleep()'s Return Value is None,If you run this:ConsoleWrite(Sleep(500) & @CRLF)oÝ÷ Û*.®¦ëpY[z«¨·Z®¢Ô¨"¯zf§j¶.qÇvÊ&zØb®¶sdÆö6Àb33c´SÓ¤Æö6Àb33cµFÖW#ÕFÖW$æB¤Fð 6öç6öÆUw&FRgV÷C¶gV÷C²fײ5$Äb¥VçFÂð b33c´SÓ÷"ð FÖW$Ffbb33cµFÖW"fwC³Ó÷"6ÆVWS£ðCan you see what I added?I changed Sleep(500) to (Sleep(500)*0), and now it's working.The only weird thing that I don't understand, is why I get 4 "hi"s in the output, instead of just 2..the function is limited to 1 second, via the (TimerDiff($Timer)>=1000) part,so If it Sleeps for 500ms, then it means I should get 2 "hi"s, not 4, no?what am I missing? Edited June 11, 2009 by Zohar Link to comment Share on other sites More sharing options...
Malkey Posted June 11, 2009 Share Posted June 11, 2009 Here's a simplified block, for others to be able to test it: Local $E=1 Local $Timer=TimerInit() Do ConsoleWrite("hi" & @CRLF) Until( _ ($E=0) Or _ ( (TimerDiff($Timer)>3000) Or Sleep(500) ) _ ) This small block, has the Sleep(500) in the condition line, and is not working. what do I need to do, to make it work? (apart from moving the Sleep(500) back to the code lines of course)Help file is wrong. Sleep returns 1 on completion of Sleep function. While Sleep(10) Wend or Do Until Not Sleep(10) Both work. Try ; Local $E = 1 Local $Timer = TimerInit() Do ConsoleWrite("hi" & @CRLF) Until ( _ ($E = 0) Or _ ((TimerDiff($Timer) > 3000) Or Not Sleep(500)) _ ) ; Link to comment Share on other sites More sharing options...
Zohar Posted June 11, 2009 Author Share Posted June 11, 2009 Thanks Malkey.. that's what I found out in last post. however you will see I still have a small bug with the number of "hi"s being printed.. Do you have any idea why? Link to comment Share on other sites More sharing options...
Zohar Posted June 11, 2009 Author Share Posted June 11, 2009 ok apparently it happens because Sleep() sleeps slightly less than it should. So it is not related to the trick we've done here. so ok, it means it's good. thank you all Link to comment Share on other sites More sharing options...
Malkey Posted June 11, 2009 Share Posted June 11, 2009 ok apparently it happens because Sleep() sleeps slightly less than it should. So it is not related to the trick we've done here. so ok, it means it's good. thank you allYou must have done this also to see what was happening.; Local $E = 1 Local $Timer = TimerInit() Do ConsoleWrite("hi " & TimerDiff($Timer) & @CRLF) Until ( _ ($E = 0) Or _ ((TimerDiff($Timer) > 3000) Or Not Sleep(500)) _ ) ; Link to comment Share on other sites More sharing options...
DCCD Posted June 11, 2009 Share Posted June 11, 2009 other ver $how_many = 3; then Exit Local $Timer = TimerInit() $i = 0 Do ConsoleWrite("hi" & @CRLF) $i = $i + 1 Until ( _ ($i = $how_many) Or _ ((TimerDiff($Timer) > 3000) Or Not Sleep(500)) _ ) ; [u][font=Arial Black]M[/font]y Blog, AVSS Parts[/u][font=Arial Black]Else[/font][font=Arial Black]L[/font]ibya Linux Users Group Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now