Jump to content

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


Recommended Posts

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
Link to comment
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.

Link to comment
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)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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)) _
        )
;
Link to comment
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)) _
        )
;
Link to comment
Share on other sites

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)) _
        )
;
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...