Jump to content

2 Whiles and 2 Sleeps


 Share

Recommended Posts

While (1) 
Sleep (5000)
; Do something 1
WEnd

While (2) 
Sleep (10000)
; Do something 2
WEnd

I want to do 2 actions like this. Action 1 is to wait for 5 secs and action 2 is to wait for 10 secs before performing action. I tried my code but the program only did the action 1 ? Can anyone tell me what the problem is please ? Thanks

Link to comment
Share on other sites

Of course it did only Action1 because

While (1)

Sleep (5000)

; Do something 1

WEnd

is an endless loop.

Let me get this right ... so you want A1 to be performed every 5 sec and A2 every 10 sec ... but it will come to the following situation: A1 to be executed at the same time with A2 ... and that is not possible. It is possible if you accept a delay between their executions.

What you will have to do is to use a counter.

For your ... erm ... vague and particular case (5 sec and 10 sec) this will do the trick:

$count = 0
While 1
    Sleep(5000)
    $count +=1
    $time = $count*5
    If Mod ($count, 2) = 1 Then
        MsgBox(0, "time: "&$time&" sec", "Action 1", 2)
    Else
        MsgBox(0, "time: "&$time&" sec", "Action 1 and Action2", 2)
    EndIf
WEnd

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Thank you very much for your answer and it works very well :lmao:. But if I want to set the sleep time for action 1 is 3600000 ( 3600s or 60 mins ) and for action 2 is 900000 ( 900s or 15 mins ), is it available to use this script again :whistle: ?

Link to comment
Share on other sites

Thank you very much for your answer and it works very well :lmao:. But if I want to set the sleep time for action 1 is 3600000 ( 3600s or 60 mins ) and for action 2 is 900000 ( 900s or 15 mins ), is it available to use this script again :whistle: ?

Just put you time checks all in the same loop. You can add as many timers to this loop as you need:

$Time_1 = TimerInit()
$Time_2 = TimerInit()

While 1
    If TimerDiff($Time_1) > 3600000 Then
        ; Action One Goes Here
        $Time_1 = TimerInit()
    EndIf
    
    If TimerDiff($Time_2) > 900000 Then
        ; Action Two Goes Here
        $Time_2 = TimerInit()
    EndIf
    
    Sleep(20)
WEnd

This method can break down if the actions performed take so long that they interfere with checking the other timers. Then you have to get more complicated, using AdLibEnable() to interrupt what's going on and check for other actions.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Just put you time checks all in the same loop. You can add as many timers to this loop as you need:

$Time_1 = TimerInit()
$Time_2 = TimerInit()

While 1
    If TimerDiff($Time_1) > 3600000 Then
        ; Action One Goes Here
        $Time_1 = TimerInit()
    EndIf
    
    If TimerDiff($Time_2) > 900000 Then
        ; Action Two Goes Here
        $Time_2 = TimerInit()
    EndIf
    
    Sleep(20)
WEnd

This method can break down if the actions performed take so long that they interfere with checking the other timers. Then you have to get more complicated, using AdLibEnable() to interrupt what's going on and check for other actions.

:lmao:

The solution PsaltyDS gave you works better in this case considering the large amount of time you need to wait for an action (I imagine your script is doing something else meanwhile).

I have tried out and the solution of PsaltyDS and it works very well. Thank you for all your helps :whistle:
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...