Sign in to follow this  
Followers 0
Zohar

[Solved] How to put a "Sleep()" inside a While block's Condition

14 posts in this topic

#1 ·  Posted (edited)

Hello

This 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 by Zohar

Share this post


Link to post
Share on other sites



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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Ohhh ok, sorry about that. Just trying to figure out why Sleep() doesn't work where it is at now

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Sleep isn't a condition/expression ... that makes no sense actually :-(

Edited by Inverted

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by Zohar

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

#9 ·  Posted (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..

BTW

It 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? :D

Edited by Zohar

Share this post


Link to post
Share on other sites

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)) _
        )
;

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

You 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)) _
        )
;

Share this post


Link to post
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
Sign in to follow this  
Followers 0